Short essential how-to list for the complete Linux newbie

Forum Forums General Tips and Tricks Short essential how-to list for the complete Linux newbie

  • This topic has 66 replies, 25 voices, and was last updated Dec 6-3:36 pm by PPC.
Viewing 7 posts - 61 through 67 (of 67 total)
  • Author
    Posts
  • #93750
    Member
    PPC
      Helpful
      Up
      1
      ::

      @caprea : Thanks!

      In non geek language, Rantanplan, try this:

      antiX Menu > Terminal

      After you have run (and enter your password if asked to):

      sudo apt update

      Run this command:

      apt-cache policy wallpaper-antix

      If the installed “wallpaper-antiX” is older than the available one (I mean, it has a lower version number) then install the newer version. You can do it without updating the entire system:

      sudo apt install wallpaper-antiX

      When the install process finishes, try using the Wallpaper selection tool- it should have the “No wallpaper” option available!

      P.

      • This reply was modified 2 months, 1 week ago by PPC.
      #93754
      Member
      Rantanplan
        Helpful
        Up
        0
        ::

        Sir professor PPC 😉

        all is good :

        Thank you PPC and caprea

        Attachments:

        Vive antiX !
        Vive le Groland !

        #93757
        Moderator
        Brian Masinick
          Helpful
          Up
          1
          ::

          @PPC: and @Rantanplan:

          I love the collaboration, and I particularly appreciate the recognition given to PPC as “Sir Professor”!

          I’m glad to see such recognition, and I thank @Rantanplan: for the words of recognition and @PPC: for continually contributing ideas and features worthy of professorship!

          The HOWTO list and the tools are among the many helpful additions to the community. Thank you (to you and others) who take the time to provide tools, tutorials and other ways to share the goodness of antiX – naturally giving thanks for the outstanding vision and engineering skills that anticapitalista has provided for many years!

          Brian
          Or "The Mas"

          #93902
          Member
          Joehair
            Helpful
            Up
            0
            ::

            Great guide. Can you help me out though? Can you provide a step by step guide to dual boot antix with windows 10? I installed windows 10 on my 500gb hard disk and left around 120gb for antiX. I tried to dual boot it and i got the live usb running but when i tried the custom install i couldn’t make a home partition because it said that i couldn’t make any more primary partition. I’m too afraid to just install it on a root partition without a home partition because i might mess it up. Can someone help me out here?

            #93965
            Member
            PPC
              Helpful
              Up
              0
              ::

              Thanks to @Rantanplan, I found a small app that can be handy:

              Do you need to quickly generate a secure password (for example, to use on a web service, that has to include a number or a symbol)?

              pwgen to the rescue! Install it using synaptic or:

              antiX menu > Terminal

              if you have not updated you system recently, do so with

              sudo apt update

              then:
              sudo apt install pwgen

              (as always, enter your password, if asked to)

              Once the app is installed, on the terminal run pwgen with some nice settings (thanks for the example, @Robin):

              pwgen -cyns1 20 1

              Are you to lazy to copy the generated password? If xclip is installed on your system, simply run this command instead:

              pwgen -cyns1 20 1 | xclip -selection "clipboard"

              Now, all you have to do is paste the password where you need it.

              Note: always take note of any passwords you use on on-line, or off-line services!

              P.

              #94528
              Member
              PPC
                Helpful
                Up
                0
                ::

                Mastering the Terminal

                First of all – the Terminal is not for hackers, or for hard core computer users. To use most Linux distributions (distros), mainly antiX, you do not need to know how to use it. The only daily task that you may need to use the Terminal, that antiX 21/22 currently has no GUI to, is installing off-line .deb files. And even so, you do not need to know anything about the terminal, except write three simple words, end up with a space, drag and drop the .deb file you want to install, press enter and enter you password, if asked to- not exacly rocket science.

                *Why use the terminal?
                The terminal is recommended when trying to solve problems because it’s a universal way, and easy to use, by the people that is giving the instructions on how to solve a problem- it works the same, no matter if you are using antiX with IceWM, JWM, Fluxbox, FT10 on top of any of those Window Managers, an Desktop Envrironment, whatever.
                It allows no room for mistakes – If I want to show you the contents of the zxy.desktop file I usually have 2 choices. I can tell you this:

                1- “Click the antiX Menu, open the File Manager, navigate to the folder called /usr/share/applications, then right click the zxy.desktop file, then choose “Open” and then Geany to see that file’s content”

                OR

                2- antiX Menu > Terminal and run this command:
                geany /usr/share/applications/zxy.desktop

                So, which one of this options is easier for me to write? Which one of this allows for less margin of error when a user is trying to interpret the instructions? Instructions to be run in the terminal are always the same, and most of the time are faster to follow, especially if you know what do to or you are copying and pasting instructions. You do not need to know what you are pasting- but you do need to trust the person that is giving you the instructions. One single command can simply wipe way your system- some very evil people used to recommend newbie users to run it when they asked for help, just for the fun of it, on other forums. Some people are just like that- they think they are smart- but they forget they also once, knew nothing and had to learn from scratch. Nuking someone’s system, just for the fun of it, teaches that person 2 things: that a command can destroy a system AND never to trust anyone on-line.

                I always recommend that if you do not know what a command does, and, specially if you do not fully trust the people giving you the instructions, check on-line what each part of the command does, and only then run it.

                But don’t be alarmed by the fact that the Terminal can be used to destroy your system. The same can be done using the menu, running the right app and clicking the right options… There’s risk in anything we do, in a society… but we need to trust each other, right?

                * What can you do in the terminal?
                Hum… pretty much anything: you can use it to copy, move or remove one or more files or folders. You can automate the creation of multiple files and folders. You can search for files. You can edit text files. You can (with some constraints) browse the web, see pictures, hear audio files, stream music, use instant messaging services, use File Managers, play some games, use a Word Processor (that can save files to .odt format, the default file format for LibreOffice, that can be also read by almost any Word Processor in existence, including MS Word), use a Spreadsheet program, create and view simple Presentations, use a Calendar, send and read E-mails…

                “But it’s ugly! And you have to type all the time! Why should I do it?” you may ask. Simple. Because, once you know how to do it, it not just works across almost every single computer using antiX, but it also works the same on a brand new computer and in a computer that’s over 30 years old. And it works fast!

                *Some conventions that the Linux Terminal uses:
                -The “prompt” (the line where you are able to write in) starts with a character, before the cursor. “$” means that you are using the terminal as a normal (non root user). If you are a root user you’ll see the “#” before the cursor.

                NOTE: when following any command, given here in the forum or in any web page, no not type the “$” or “#” in the start of the command – if it’s there, it’s to let you know that you should run the command as a regular or root user. NEVER RUN COMMANDS AS ROOT USER UNLESS OTHER WISE TOLD TO!!! It can be dangerous for your system.

                – Usually the prompt also displays the user name the symbol “@”, the name of the computer, the simbol “:” and the folder where you are. If no folder is in the prompt, then you should be in your home folder. Note that the symbol “~” stands for your home folder. It’s the same as reading or writing “/home/[user_name]”

                – Unlike in Windows, that decided to the the opposite of what was the standard, in Linux folder are separated by the “/” symbol. Do you have any doubt “/” to separate stuff is the standard? Just look at any web address…

                Example: when I say “/usr/share/applications/zxy.desktop” You can read it from right to left: it’s the zxy.desktop file, inside the “applications” folder, that is inside the “share” folder that is inside the “usr” folder.
                When read from left to write, it’s the full path that you have to “walk to” to reach your file.

                Example: If your prompt looks like this:

                ppc@dhcppc1:~/cac
                $ 

                The “$” means that you are a regular (non root) user, called “ppc”, and that the computer’s name is “hcppc1” and that currently we are in the folder “cac” that is inside ppc’s home folder.

                I’m not going to explain what “folders” are. If you need someone to tell you that, probably you never really used a computer before…

                But I’m going to explain how folders work in Linux, when compared to Windows…

                On Windows, usually the main Hard drive (or ssd drive) is called “c:”. Your documents are stored in a folder that is something like this:
                C:\Users\[Your_user_name]\Documents

                On Linux we do not need to identify the drive’s name (and we refer to drives using a different convention anyway). So there is no “C:” on Linux. The main “hard drive” is just “/”. In Linux, for safety reasons, regular (non root) users can only write (alter or create) files or folders inside their own “Home folder”.
                A user’s home folder in Linux is /home/[User_name]/ And can be shorted to ~/

                Please note: “/home/” is not your home folder, strange I know, but it’s the place where the home folders for all users in your computer are stored.

                Ex: /home/ppc/ is my home folder, in this computer. /home/marcelo is Marcelo’s home folder, in this computer. Only I and someone with elevated privileges (a root user or a normal user using sudo) can change things inside my Home folder. The same applies to Marcelo’s home folder. Only he or a root user can change things inside his home folder.

                When you start a Terminal, usually you start in your own home folder.

                *Listing contents of current folder:

                Use the “dir” or “ls” (dir stands for directory and ls for list). On antiX’s terminal it’s best to use the “ls” command, because it outputs a color coded result. Fordes are blue. If you run simply “ls” you get an alphabetical list of everything that’s in the current folder, mixing sub-folders and files.

                Note1: you can scroll back the contents of the terminal, to see something that rolled out of the screen, handy in case you have many files listed.

                Note2: you can repeat previous commands by pressing the up arrow in your cursor keys. You can keep pressing it, going further back in the history of commands you have run on the terminal. This is particulary handy if you just want to repeat a recent command, or you messed up anything, while typing the previous command. Just press the up key and then move left and fix the command (adding or deleting what was wrong).

                If you want to view one page of information at a time, add, on the end of the command you want to run “| more”. Ex: ls |more lists one page of the folder’s content at a time. You have to press “enter” to move to the next line or “space” to move to the next page. This works for just about any command that outputs results in the terminal.

                You can also just display files that fit your criteria. Do you want to display just .txt files that exist in the current folder? Simple:

                ls *.txt

                Try to do that with a graphical File Manager… Harder, hum?

                *Running an application using the terminal:
                It’s easy. Just type it’s name and press enter.
                Ex: if I want to start Geany, all I have to do is

                geany

                and press enter.
                You don’t even need to know the full name of the command. Just type the first letters and press the “tab” key, and the Terminal will autocomplete the command, if there’s only one command that starts with the letters you typed or it will offer you some suggestions, if more than one applications start with those letters.
                Ex: type “gea” and press tab. The terminal will auto complete the command and “geany” will appear, replacing what you just types. Press enter, to run geany.

                *Opening a file from inside the current folder:
                This may sound strange, but if you want to run a file inside the folder you are in, you can’t just type it’s name. You have to precede it with “./” so the OS knows that you mean the file with that name that is inside the same folder that you currently are on.

                Ex: If I want to see the text file called lusiadas.txt, that it inside the folder where I am, I can do it using geany, like so:

                geany ./lusiadas.txt

                Note1: unlike in other OS, Linux does not need file extensions to know what file type a file is. You can basically have any file without extension. Linux will know what file type it is. Extensions are there so users know what kind of file it is… A text file does not have to end in .txt. The system will know what is the correct file to open it.

                Note2: Linux is case sensitive: this means that, if the command to run Geany is “geany”, “Geany”, gEany”, etc, won’t work. this applies to both commands and regular files. You can have, in the same folder “Lusiadas.txt” and “lusiadas.txt” they are different files.

                Note2: to access a file or folder that is inside your home folder you don’t need to type /home/[your_user_name]/ before the file name. You can shorten it to ~/

                Ex:
                geany ~/lusiadas.txt

                will open the file called “lusiadas.txt” that is in my home folder, even if there’s a different file called “lusiadas.txt” on the folder where I’m right now.

                *Reading a text file from inside the terminal
                Sometimes, you don’t need to open a text editor to read a file that’s just a couple of lines of text. The “cat” command does that for you, listing the contents of a file, right inside the terminal.

                ex:
                cat ./lusiadas.txt

                *Changing folders:
                To change the folder, use the “cd” command (it stand for “change directory”). “cd” by itself always sends you to your home folder, no matter where you are.

                Ex:
                cd /usr/share/applications

                Brings me to that folder. I can list it with “ls”. If you know the folder where you want to go, sometimes it’s faster to type the name in the terminal, specially if you autocomplete it using the “tab” key…

                Continuing our example…
                cd ./antix/

                …Moves you to the subfolder called “antix” inside the folder where you currently are.

                To move back to the folder where the sub folder is in (I mean: to move to the previous folder on the folder tree), do it with:
                cd ..

                In our example, you were in the /usr/share/applications/antix folder. After “cd..” you get to the /usr/share/applications/ folder.

                * Copying files:
                You can copy one or more files with the “cp” command. It works like this: cp [path_to_the_original_file] [full_path_to_the_place_where_you_want_the_copy_to_be]

                * Creating files:
                You can create (an empty) file with the “touch” command. It works like this: touch [full_path_to_the_place_where_you_want_the_file_to_be]

                * Creating a folder:
                You can create (an empty) folder with the “mkdir” command (it means “MaKe DIRectory”). It works like this “mkdir [full_path_to_the_place_where_you_want_the_file_to_be]”

                Note: as always “./” tells the Terminal that you mean to use the same folder as you currently are.

                Lets try out some examples:

                cd

                (to make sure that you are in your home folder)

                mkdir ./ppcsfolder1

                (this creates a folder called “ppcsfolder1” inside your home folder) In the case of mkdir it always creates the folder inside the current folder, so this also works:

                mkdir ppcsfolder1

                Lets move to that new folder. We can do it using several ways. Try this:

                cd ./ppcsfolder1

                (this tells the terminal that you want to move to the “ppcsfolder1” that is inside the current folder). Because “ppcsfolder1” is inside our home, we could also have used cd ~/ppcsfolder1. Also if you just use cd with a folder name, the Terminal assumes that you mean the subfolder that’s inside the current folder, so this also works:
                cd ppcsfolder1

                There are lots of ways of doing the same thing inside the terminal.

                See the prompt? It should show you that you are in “~/ppcsfolder1”

                Lets create a file here:
                touch ./lusiadas.txt

                You can make sure you created it by listing the folder’s contents with “ls”.

                Do you want to edit that file using geany? You can do it, like I showed in the previous example:

                geany ./lusiadas.txt

                If you are editing the file, type “abc” inside it and save it. Close geany.

                Do you want to take a look inside the file, directly from the terminal?

                cat ./lusiadas.txt

                Is the file too big? Better read it page by page, piping it through the “more” command:
                cat ./lusiadas.txt |more

                * Creating sub-folders
                You can create a subfolder just like you create folders.

                mkdir January

                Will create a folder called January inside the current folder.
                Until now you may be scratching your head, thinking how using the terminal is faster than using a Graphical File Manager, right?

                Lets say that you want to create folder to store files that you use, for every month of the year (for storing receipts, etc). It would take you lots of time, creating one folder at a time, in your FM. On the terminal? You can do it with a single line, because you can create as many folders as you like using the same mkdir command:

                mkdir January February March April May June July August September October November December

                You can check if you created the folders, running the “ls” command.

                Do you see now, how the terminal can simply be faster, sometimes, than using a Graphical User Interface (GUI)?

                Let’s move inside “January”‘s folder:
                cd January

                Let’s create a few files:
                touch 1.txt 2.txt 3.txt

                Just like the mkdir, the “touch” command allows you to create more than one thing at the same time. Once again, try doing that using a GUI… It takes a lot more time.

                Lets try to copy a file from January to February:
                cp 1.txt ../February/

                This copies the file called “1.txt” in from the current folder to the folder called “February” that is on the same main folder as the current folder is.
                It’s simpler than typing this, that does exactly the same thing, to the file called “2.txt” that is in the current folder:

                cp ./2.txt ~/ppcsfolder1/February/

                You can examine the contents of this folders using a File Manager, while you are running the command in the Terminal.

                If you want to move a file from one folder to another you can use the “mv” command:

                mv ./3.txt ~/ppcsfolder1/February/

                *Deleting a file
                You can delete files with the “rm” command (it stands for ReMove), like so “rm [file_you_want_to_delete_inside_the_current_folder]” or “del [full_path_to_the_file_you_want_to_delete]

                Make sure that you still are in the “January” folder and delete the “2.txt” file:
                rm 2.txt

                *Deleting a folder
                You can delete folders with the same “rm” command, it works for files and folders, but you have to use it with a “flag” (an option that sends instructions to a command, usually, if a flag is one letter only, it’s preceded by the “-” sign. If it’s a word, it’s preceded by two dashes, not just one. also, if flags are just one letter, sometimes you can group several of those letters together, after a “-“).
                We can use a flag to tell rm to delete a folder like so: “rm -rf [name_of_folder]” (-r flag stands for “Remove Folder recursively” and the -f flag stands for “Force, never ask”, mixing both flags- yes, you can do that- you get “-rf” ). To make sure that you do not delete a folder by mistake, you do have to specify you mean the folder. This ensures that, if you have, in the same folder a subfolder and a file with the same name, you have different ways to delete the file and the folder.

                Let’s test it. Make sure that you are inside the “January” folder and then set back to the main “ppcsfolder1”. You can do it with this command:

                cd ..

                The prompt should tell you that you currently are in “~/ppcsfolder1”
                Let’s delete “December”

                rm -rf December

                You can check running the ls command. December is gone 🙂

                You now know you to move around your folders, run commands, open files and create files, folders and copy files and move files, and delete files and folder, using the Terminal.

                *Renaming a file or a folder
                If you are just learning about the Terminal, this will probably blow your mind. On Linux, you rename files or folders by moving them to a new name. Yes. Weird, right?

                Continuing our example, to test this, lets make sure that we are in the January folder

                cd ~/ppcsfolder1/January

                If you run “ls” you can make sure that the “1.txt” file is still there. Lets rename it to “a.txt”:

                mv 1.txt a.txt

                (when moving, even if you don’t use ./ before the file name, the command assumes you are moving (or in this case renaming) the file with that name in the current folder.

                * Finding a file
                There are several ways to find files in the terminal. One way if using the “locate” command.
                To find all text documents on your computer:
                locate *.txt

                Of course, that if there are too many files, you can pipe the result to the more command:

                locate *.txt |more

                Do you want to find a text file that you are sure includes the word “love” somewhere in it’s title?

                locate *love*.txt
                … and you’ll find love 🙂

                *Using files with spaces inside their name:
                To use a file that includes spaces in it’s name, you have to place it inside simple or double quotes
                Ex: os lusiadas.txt
                To refer to this file you’ll have to type “os lusiadas.txt”
                Why is that?
                Because the terminal assumes that regular files do not have spaces, and that everything after a space is a new command.

                Ex of what you should not do:
                cat os lusiadas.txt

                Will try to read the first thing until the first space: in our case “os”. The Terminal assumes you mean to say “cat os”

                Ex of what you should do:
                cat "os lusiadas.txt"
                (please note that due to the way the forum works, I had to use double quotes, single quotes do not appear correctly)

                *Using elevated privileges- the sudo command:
                If you want to run any command that only someone with administration rights/privileges can, you have to precede the command with the word “sudo”, and enter your root password, if asked to (the Terminal remember the password you timed for a few minutes, to save you the trouble of repeating it all the time).

                *If you want to run several commands on the same “command”
                To run several commands at the same time separate them with “&”
                To run several commands, one after the other, start with the first command, then the second one, etc, separate them with “&&”

                *Updating your Linux using the terminal:
                antiX is a distro based in Debian, this means that it uses the same way to manage packages (files that install applications or stuff applications or the system needs to run, for example, like fonts, drivers, etc). Debian’s and antiX’s package manager is “apt
                Since using apt to change your system (and updating it does change it), it can’t be run by regular (non root) users, so you have the precede the command with “sudo”
                Also since apt can perform several tasks, you’ll have to say what you want apt to do (it’s a kind of “flag”, without – or –, just words).

                Example:
                Updating the list of packages available in the repository (the list of everything you can install using apt). NOTE: YOU HAVE TO RUN THIS COMMAND to be able to install anything that’s in the repository, this means that even if you want to install a .deb that you have in your hard drive, and it requires some dependencies that are not in your system, that .deb file won’t install if you have no list of available packages in your system…

                sudo apt update

                Wait until the procedure finishes. You know when a command is finished when you see the prompt again.

                Now that you have updated the list of available packages, you can upgrade you system, and install the latest versions of the packages that you have installed in your computer (for example, this ensures that you have the latest version of firefox-esr, geany, zzzfm, etc)

                sudo apt upgrade

                Note that every time you want to run something that only the root user can do, you ALWAYS have to use sudo, even if the commands are in the same line:

                sudo apt update && sudo apt upgrade

                Will first check for updates, then after that is finished, upgrade your system

                * Installing a .deb package that you have on your hard drive:

                sudo apt install [full_path_to_the_package.deb]

                Don’t forget if the package is called xyz.deb and it’s in your current folder, you can install it with:

                sudo apt install ./xyz.deb

                If that file is inside your home folder, you can use:

                sudo apt install ~/xyz.deb

                *Finding and installing a package from the repositories

                IMPORTANT: To find and install anything from the repository, don’t forget that you have to have run, at least once the “sudo apt update” command!!!

                To find anything you want in the repositories (repos) that list all available packages to install in antiX: apt search [what_you_want_to_find]

                Ex:
                apt search vlc
                … will search if VLC (media player) can be installed in your computer. You’ll get many hit’s because there are packages for the app itself, data files, plugins, and other apps that refer “vlc” in their description.
                Please note that you do not need to use sudo, because you are not changing your system, just “browsing the store”.
                The first word, in green is the package’s name, the name that you have to install if you want that app. Unlike in a usual modern GUI App Store, you’ll have to ignore binary files, language files and plug-ins (if you don’t need translations or plugins, I mean) and find the package you need, in order to install the app. In our case, it’s not that hard. It’s the “vlc” package.
                NOTE: package names do not usually have capital letters, even if the app they install has capital letters (like VLC)

                Installing an app from the repository: once you know the name of the app’s package, it’s easy: “sudo apt [name_of_the_package]
                In our example:

                sudo apt install vlc
                Now you need to use sudo, because you’ll be making changes to your system. If asked to, of course you have to enter your password.

                *Do you want to uninstall an application?

                First you need to know that’s the app’s package (you can search for it, like in the previous example). If you know the name of the package, it’s simple: sudo apt purge [name_of_the_packahe_to_be_removed_from_your_system]

                Ex: If you have VLC installed on your system and you want to “uninstall” it:
                sudo apt purge vlc

                *Getting information on your system- the inxi command
                If you ask for help here in the forum, you probably should give some info about your devide. The inxi command does just that.
                Use it with the recommended flags, and add the results to your post, so folks here know how to help you.

                Tip:
                You can save the output of most command to a file adding, to the end of the comand this ” > [path_to_the_file_that_stores_the_result_of_the_command]”

                Example:
                inxi > ~/myfile.txt

                will not display the result of the command, but save it directly to a file called “myfile.txt” in your home folder.
                You can read that file with:
                cat ~/myfile.txt

                *Need help?

                Using the terminal is only a matter of learning the commands you need, and the options you need to use with those commands. You already know the basics.
                Usually you can type the name of the command followed by -h or [two dashes] help to read it’s help file. Many commands have manuals: use the “man” command followed by the name of the command whose manual you want to read (you can quit the manual by pressing “q”).
                Ex: To read “ls”‘s manual:

                man ls

                *What are “scripts”?
                A Bash script is an executable (text) file that has one or more commands inside it. It’s a way to automate what you want to do. You can create a simple script, for example, to install several applications, via something like this: sudo apt install app1 app2 app3 app4, etc… You can create a script that creates several folders, or backup several folders or folders to a certain folder, etc. There’s no end to what you can do. Countless “apps” included in antiX are just very complex bash scripts… Terminal commands allow you to automate what you want to do, saving you effort on the long run.

                Final note: you can usually drag and drop files to the terminal, and they appear right where the cursor is. You can also select anything from the terminal and copy it, pasting where you want it to be, saving you the trouble of having to write down what you see in the screen. On Roxterm, the default Terminal in antiX, you can copy and paste by clicking the right mouse button and select what you want to do.
                Ex: you can search for a text file using “locate”, use your mouse to select and copy that result and then open it, by typing geany, a space, and pasting the file name and pressing enter.
                You can adapt that to run any kind of file with the program that runs it… And don’t forget you can always run previous commands using the up arrow key, and “tab” autocompletes.
                You can run several commands, one after another, by copying several lines of text of commands and pasting them to the terminal (see the next example)
                Also, I’ve not yet told you that, but if you have been paying attention and reading between the lines, you know this: you do not have to be in the same directory where you want to perform changes.
                Ex: you can be in your home folder and read a file that is somewhere else:

                 cd
                 cat /usr/share/applications/rox-filer.desktop

                Will let you read the contents of rox-filer’s .desktop file without having to be in the same folder as that file. Now… Can you do that using a GUI File Manager?
                You can create, run, copy, move, rename and delete files and folders without moving to the folder where they are. That is one of the reasons the Terminal is so powerful.

                Not that hard, right?

                That’s basically it. You now know your way around the Terminal, and you have the tools to learn about any command that comes your way.

                P.

                • This reply was modified 1 month, 4 weeks ago by PPC.
                • This reply was modified 1 month, 4 weeks ago by PPC.
                • This reply was modified 1 month, 4 weeks ago by PPC.
                #95003
                Member
                PPC
                  Helpful
                  Up
                  0
                  ::

                  This is the second (and for now) last installment of Mastering the Terminal:

                  * What are Permissions
                  From my previous post, was probably already guessed that is a way that the OS uses to find out if you can read, write or execute certain files – to do that to files (like system files) that belong the the root user, you have to either be the “root” user or a normal user with elevated previleges (i.e using the prefix “sudo” and entering, if asked to, the root password).
                  How does the OS know if you can or can’t access or run a certain file? Because each file and folder always have their permitions stored, letting the system know if you can read, write, rename, delete or execute a file or folder.
                  You can change the file permission of any file or folder, as long as you have the right to do so – this means that normal users can only change permissions of files and folders that belong to them and “root” users can change any file they want to (that’s why running the system in “root” mode can be dangerous).

                  [Note: Windows security was so flawed, back when I used it, because everything basically ran in “root” mode- if you got a virus, it affected any system file it wanted because it could do so. I think that things improved a little bit on that regard, but I’ve not used that OS in a serious way for a dozen years now.]

                  You can change file permissions in your file manager or in the terminal. Example, if you downloaded an Appimage and want to run it, you have to change that file’s permissions so it’s marked as executable, or the system won’t let you run it (it’s a security feature, forcing you to always make sure you really want to run something that may be- or not- dangerous. It’s like the system is asking you “Are you really sure you want to always be able to run this app? This kinds of files may be a virus, or spyware!!!”

                  Once you mark one or more files as executable, you won’t ever need to do it again, on that Linux installation – the system remembers the permission you gave that file forever.

                  Basic file permissions (in the order they are listed in the terminal):
                  “r” stands for “read” permission – allowing you to access or read that file/folder but not change it
                  “w” stands for “write” permission – allowing you to read and change that file/folder
                  “x” stands for “eXecute” permission – if the file is a binary executable file or a script, this allows you to run it
                  “-” means that there is no permission (if the first character of the permission is -, you have no “read” permition,if it’s the seconds, you have no write permitison. if it’s the third, you have no permission to execute the file.

                  * How to list file permissions:
                  ls -l

                  This lists the file type, the permissions of the files (The permissions are displayed 3 times, for the user, for the group that the owner belongs to and for “other”), the owner of the file, the group of the owner of that file, the date of the file’s creation and the file’s name.

                  Note: You can represent permissions using numbers (the “octal notation”), but I won’t get into that detail here.

                  Changing permission on the terminal- the “chmod” command:

                  chmode [permissions] [file]

                  or in a more detailed way:

                  chmode u=[letters_of_permissions], g=[letters_of_permissions], o=[letters_of_permissions] [file]

                  This will set permissions for a file for the current user ( “u”), the current user’s group (“g”) and all other users (“o”)

                  Example: lets move to your user’s home folder, create a file, check out it’s permissions and then change them:

                  cd
                  touch ppcsexamplefile.txt
                  ls  -l ppcsexamplefile.txt

                  The result should similar to this:
                  -rw-r--r-- 1 ppc ppc 0 dez 5 15:38 ppcsexamplefile.txt

                  How to interpret this:
                  – ignore the first “-”
                  – the “rw-” that come after that are the permissions for that file, under the current user (ppc). I can read that file, write to it (change it) but not run it, if it has a script inside it.
                  – “r–” means the group that I belong to can only read this file, but not write to it or run it
                  – the last “r–” means that all other users that do not belong to my group can only read this file, but not write to it or run it

                  -In my case “ppc ppc” means I’m the user ppc (the first word), and that I belong to the group of users called also ppc (the second word)

                  -then you have the date of creation of the file (month, day, hours and minutes) and then the file’s name.

                  You can make the file executable, adding the “execute” permission like so:

                  chmod +x ppcsexamplefile.txt

                  To check the result of this command, you can run:

                  ls -l ppcsexamplefile.txt

                  Now you see that you added the “x” permission, for that file, for your user, your group of users and any Other users (that’s why you get the 3 “x”)

                  This means that you can now place a script inside this file and run it. But it’s better to undo that change, and remove executable permissions:

                  chmod -x ppcsexamplefile.txt

                  * Making an appimage file executable:

                  chmode a+x [appImage_file.Appimage]

                  Personal note: I always use zzzfm File Manager to change permissions before I run an Appimage or a script. Sometimes, using a GUI is indeed faster.

                  * Writing something in the screen or to a file- the echo command:
                  The echo command is very used, but it took a while for me to grasp it’s usefulness. why do we need a command that writes something on the screen?

                  Ex:
                  echo "Hello World"

                  just makes «Hello World» appear on screen.

                  You can change lines, inside that text, using the -e flag and using “\n” in the place, inside your text, where you want the new line to be inserted.

                  Ex:

                  echo -e "Hello \nWorld"

                  Will show:

                    Hello
                     World

                  At first I though- “well, if I wanted that, I would just type that text straight into the terminal. when I began using the terminal more and more and then I started trying to automate things with, first, strings of commands I copied and pasted and then saving them as scripts and running those scripts, I understood that the “echo” command is perfect to display warnings that something is about to happen or that it just happened.

                  You can update you system you system using several ways. One is this:

                  sudo apt update && sudo apt dist-upgrade

                  But if you never used the terminal or ran this commands, they may look scary. So you can display a mini-help text before you run the command and then a warning when it’s finished. It’s easy:

                  echo -e "\n \n You are about to check for updates for your system.\n First the computer will update it's database to try to find if there are newer versions of the packages and apps you have installed available. If there are, you'll see a message telling you how many packages can be updated, then the computer will try to upgrade your system to those new versions.\n If asked for a password, please, enter your 'root' password and press enter.\n If asked something, you can just press the 'enter key' to accept the default answer, usually 'yes'.\n IMPORTANT: Do not close this window until instructed it's safe to do so.\n" && sudo apt update && sudo apt dist-upgrade && echo -e "\n \n The update procedure is finished. You can now safely close this window.\n"

                  NOTE: Be careful using exclamation signs in echo commands with the -e flag, Linux may expect a command and may spit out an error.

                  You now have a tiny script that anyone that never even used the terminal can run, and know exaclty what’s happening, as long as hey can read and follow instructions so simple a 8 years old could do them.

                  * Using echo to enter text inside a file, directly from the terminal, without opening it:

                  This is where the Terminal shows it’s power. Let’s say that you want to type “Hello world” inside our ppcsexamplefile.txt

                  One way to do it is launching Geany text editor, opening the file, entering the text and then save it.
                  But, as in just about anything, you can automate that, using the terminal (or a script):

                  echo "Hello World" > ~/ppcsexamplefile.txt

                  You can see the contents of the file by opening it with Geany or, more simply using the cat command:

                  cat ~/ppcsexamplefile.txt

                  Fast, isn’t it?

                  echo “[some_text]” > [your_text_file] makes sure that the ENTIRE CONTENTS of your file will be the text inside the echo command.

                  If you just want to add text (append) to the END of your file, you have to use “>>” instead of a single “>”

                  Ex:
                  echo "Hello again World" >> ~/ppcsexamplefile.txt

                  “Ok. Nice trick” you may say. Why will I ever want to insert lines of text to a file from the terminal, if I can do it using a much better looking way, a GUI text editor?”

                  You are right, of course. But what if you wanted to change a config file and add a new setting to it, example, making the xyz add start automatically every time you start your system? On the forum the instructions would be something like:
                  antiX meu > Control Centre > Icewm… > “Startup” Tab > On the very last line insert “xyz &” > Save the changes you just made to the file. xyz will automatically start the very next time you start antiX

                  The procedure takes too long and has many steps. Now the instructions to do exactly the same thing using the terminal [DO NOT RUN THIS COMMAND!!! AND IF YOU ADAPT THIS COMMAND FOR YOU USE, DON’T FORGET TO USE “>>” OR YOU WILL ERASE THE ENTIRE CONTENTS OF THE FILE!!! ] would be:

                  echo “xyz &” >>~/.icewm/startup

                  Do you see the difference? Using the Terminal sometimes is faster and eliminates any chance of making mistakes

                  Lets say you want to make the exact same change, adding xyz to the startup file of 12 or so computers in your office. How long do you think running a simple command instead of using the GUI to edit a config file will take? And, oh, yeah, you can save this command as the file “automatically_start_xyz.sh”, save it to a pen-drive, or a shared folder, make it executable and run it inside every single one of those computers.

                  But this is a dumb command, a dumb “robot”, if you will. all this command does is adding a line. If the line already exists, it will still add that line, so the computer starts xyz over and over again, if you run the command multiple times on a computer. If you want to create a smarter command, a smarter script, you have to learn more advanced tools, that make bash a kind of “programing language”:
                  You may have to learn to use “variables”, “if and then”, “loops” and commands that automatically parse text, to find out if a certain string of text already exists in a file (like “grep”).

                  *IMPORTANT NOTE: If you want to use scripts that are available in github, gitlab, etc, don’t simply use your web browser to save those files!!! They will be include text/web related info, and will not run! To save and use those scripts: open a text file in you computer, using a text editor (ex: geany), and copy and paste the contents of those scripts. Save the file (using the “.sh” extension is not required, but is advisable) and make it executable (you can do so using your File Manager, no need to use the terminal if you don’t want to. If using zzzfm: right click the file > Proprieties > Permissions > Make sure that all the boxes in the “Execute” column are checked > click the “ok” button ).

                  If you only want to grasp the basic of the terminal, you need not to read further. That’s it. There are lots of simple commands that you can learn, line “date” (that displays the current date), sleep [number_of_seconds] (that makes the terminal wait X seconds before proceeding to do something) and so on.
                  antiX includes, out of the box, some powerful command line applications, like htop (to see the system resources), ytfzf (to search through youtube videos and stream them using mpv video player), you can install the latest versions of wordgrinder (a cli word processor that can import and export .odt files, the same text document that LibreOffice uses by default), mutt (an e-mail client that works on the terminal), etc.

                  ——–

                  * More advanced tips for bash scripts:

                  My advice? If you are interested in writing scripts? Keep it as simple as you can. Use variables. And use a lot of comments.
                  In scripts, anything after “#” will be considered a comment and ignored. Make lots of comments, when trying to write a script, then later, you’ll know what each part of the script does, it’s handy, if you, like me, have bad memory.

                  Also, when creating a script, you have to always identify the shell that the script will run, on the very first line. So, your bash script should start with a line similar to this:

                  #!/bin/bash

                  …after that insert another line, starting with “#” describing what the script does, so later you, or other users, don’t have to read trough it to know what it does, what version it is, etc.

                  NEVER FORGET: if you wrote a script, change it’s permissions, so it’s executable.

                  Continuing our previous example- lets say you want to add a line to a config file, but only if that line doesn’t already exist…

                  * Checking if a file contains a string of text- the “grep” command:

                  This is a powerful command, but it’s easy to use. The basic syntax is grep [flags_like_-q] [word_you_want_to_know_exists_in_the_file] [file_that_will_be_checked]

                  Using our ~/ppcsexamplefile.txt. Let’s check if it contains any line with the word “again”, and if so, display that line:

                  grep again ~/ppcsexamplefile.txt

                  If you followed the previous examples, this command will find a line that contains that word and show us the line “Hello again World”. This does not look hard, but imagine that you are working with a huge text file, with thousands of lines and want to find lines with a particular text… Now this would be handy, right?

                  * “If then” – this makes the computer to take action only when a certain condition is met. How to use it:

                  if [condition_to_be_met]; then
                  [action_to_be_performed]
                  fi

                  The “fi” at the end closes the loop, don’t ever forget to use it. You can, of course, “nest” if statements inside one another, making the computer’s behavior be very complex.

                  Lets try to automate results for checking if a text does not exist inside a file, and if it does not, only then add it to the file. We will combine if with grep:

                    if grep -q  again  ~/ppcsexamplefile.txt; then
                       echo "The word 'again' does exists in ~/ppcsexamplefile.txt."
                    fi  

                  this runs the grep command and analyses if it has any output (the -q flag is to make it “quiet”, and do not show results on the terminal). And then warn you if the condition is met (i.e if the command inside the if statement did output any result)

                  “If” statements can be more useful if they also tell the computer what to do if the condition is not met. For that, you can use the “else”. Example:

                       if grep -q  again  ~/ppcsexamplefile.txt; then
                          echo "The word 'again' does exist in ~/ppcsexamplefile.txt."
                        else
                          echo "The word 'again' was not found inside ~/ppcsexamplefile.txt."
                       fi  

                  How does this help you? Simple. Imagine this file is a config file, and you want to make sure that, if does not already containg “xyz &”, then it’s added. Let’s adapt the example:

                       if grep -q  "xyz &"  ~/ppcsexamplefile.txt; then
                          echo "A line containing xyz &  already exists in ~/ppcsexamplefile.txt, no need to add it again"
                        else
                          echo "A line containing xyz & does not exist in ~/ppcsexamplefile.txt, adding it"
                           echo "xyz &" >>~/ppcsexamplefile.txt
                       fi  

                  Now you see this script is smarter. run it once and it detects that the text does not exit inside the file, and makes the necessary changes. After that, if you run it again, you get a warning, telling you that the line you want to add already exists inside the file.
                  Now you can adapt this as you will, to make sure that a config file always includes a certain line inside it, on it’s end. And usually the last options are the ones that are used (ex: if a config file has a line saying “toolbar=off” and later a line saying “toolbar=on”, the system will read this as meaning that the toolbar should be on)

                  * Variables

                  Imagine variables as drawers you can use to put anything inside it, no matter how large the thing is. Then, after that, when you want to use that thing, you only need to refer to the drawer (variable) where you stored it.
                  Example: Lets say your name is “Dom Afonso Henriques” (the first king of Portugal) and you need a script to display that name several times. Instead of keep typing that name, you can store it in a variable and then call that variable.

                  How to use variables: [variable_name]=[contents_stored_inside_the_variable]
                  To then refer to that variable, use $[variable_name]

                  ex:

                  king="Dom Afonso Henriques"

                  Say than now you want to see the king’s name on-screen:

                  echo $king

                  Simple, right?

                  You can use variables inside “normal” text:

                  echo "The first king of Portugal was $king. He had a very heavy sword."

                  Let’s adapt our previous example to use variables, with a tiny change, and I’ll also let you know about system variables. You should not ever create variables with spaces (because it makes it harder to refer to them and can lead to problems, use _ instead of spaces) and you should never user variables with CAPITAL LETTERS. Those are reserved for system variables.
                  To save time, while using commands, or scripts, Linux already has some useful info on some variables:
                  try this:
                  echo "My user name is $USER, so my home folder is $HOME"

                  since some commands have problems using “~” to refer to your home, let’s use the $HOME variable instead of “~”, to avoid problems.

                      command="xyz &"
                      file="$HOME/ppcsexamplefile.txt"
                           if grep -q  "$command"  $file; then
                          echo "A line containing $command  already exists in $file, no need to add it again"
                        else
                          echo "A line containing $command does not exist in $file, adding it"
                           echo "$command" >>$file
                       fi

                  Using variables saves you the trouble of typing the same thing over and over, and also reduces the chance of you making a typo.
                  Also, variables make it easy for you to change a script. In our case, we can make sure that a different line of text exists, simply by changing the variable “$command” and running the script again. To you want the script to check the contents of a different file? simply change $file’s variable content to point to the file you want and rerun the script.

                  I won’t say this is simple, but rocket science it’s not. I suck at math, but I do love logic, and scripts are as logical as Spock. If they do not work it’s because you are not giving the correct instructions.
                  The hard part is the syntax. A comma, a double quote instead of a simple quote, tiny things like that can ruin a perfectly good script.

                  *Variables that are the result of the output of a command: [variable_name]=$([command_whose_output_you_want_to_use])

                  Variables can store almost anything: text, numbers even the result of a command. Lets try this out, and learn about the “wc” command too:

                  wc $HOME/ppcsexamplefile.txt

                  This tells you the number of lines, words and characters the file has.

                  Do you want a cleaner result, showing you only the number of line and the name of the file? Use wc with the -l flag:

                  wc -l $HOME/ppcsexamplefile.txt

                  Do you want to keep just the number of lines, and cut out the file name?
                  We have several ways to do that. Remember that we can output the result of a command to a file using “>” (or, of course “>>”)? We can also input something to a command using the reverse sign: “<“. In wc’s case, when inputting a file to a command, the name of that file is not displayed in the output, so this gives you only the number of lines in the file:

                  wc -l < $HOME/ppcsexamplefile.txt

                  Let’s store the out put of that command in a variable, and use that variable in a sentence:

                    nrlines=$(wc -l < $HOME/ppcsexamplefile.txt) 
                    echo "This file has $nrlines lines."

                  * Using commands to parse text “awk” and “cut”
                  awk can be quite complex to use, but let pipe any command through it and, for example, see only the word nr X, that matters to you.

                  Using a previous example, to get only the first “word”, the nr “1” word (the number of lines) from the wc command:

                  wc -l $HOME/ppcsexamplefile.txt | awk {'print$1'}

                  Note1: There is another way to get info from an output, the “cut” command. One way to use it is: cut -d ‘[divider_of_fields]’ -f [the_field_number_you_want_to_see_the_contents]

                  in our example:

                  wc -l $HOME/ppcsexamplefile.txt |cut -d ' ' -f 1

                  Shows only the first “field” (thing) that is displayed, using a space as a divider between fields.

                  Do you want to see a more complex way to use the “cut” command?
                  You can use it to check the value of a field inside a config file (oh, yeah, and, if you know how to do it, change that value)…
                  Do you want to know how many virtual work spaces you have configured in Icewm and their names?
                  cat $HOME/.icewm/preferences|grep WorkspaceNames

                  Now you can use the cut command to show only the relevant info, not the name of the variable in the config file:

                    echo "You have this desktop(s) available under IceWM:"
                    cat $HOME/.icewm/preferences|grep WorkspaceNames|cut -d = -f 2

                  Note1: since the output of that command includes quotation marks, it’s a bit tougher to use that output in a variable, since variables with multiple words need to be inside quotation marks themselves… Hum… It’s not all roses, in bash scripts! But, if you dig long enough, you’ll there’s a way to do almost anything!

                  Note2: you can even replace words inside a text file, from the Terminal, using the “sed” command.

                  If you are serious into scripting, it would be nice to use a search engine and look for “bash sed”…

                  *General tip: If you write very long lines of code, you can split them with the “\”, press enter and continue the commands in the next line. When reading the script, Linux will interpret every line separated by that symbol as a single line.

                  *GUI Tips: while using scripts: I wrote some simple GUI’s for antiX. They all rely on the yad command. Yad displays GUI windows that can be used for many tasks, like a pop up window that displays a warning, a file selection window, to enter data, to show buttons that launch different scripts, etc. I learned the basics of using yad for the express porpoise of creating GUI’s for antix. Once you know the basics of scripting, you can start learning about yad, to produce simple (or even very complex) GUI’s for your scripts. a nice starting point is here: http://smokey01.com/yad/

                  *Examples of what you can do using not so complex scripts:

                  – If you use a laptop and sometimes you connect it to an external monitor, you can make a script that checks if an external monitor is plugged in and, if so, turn off your internal screen and use only the external one. Place that script in your startup file and, voilá, every time you start your session, the computer automagicaly uses only the external screen!

                  – You can create a list of every package you installed in your system, and use it to automate the installation of all those packages on a brand new system, without having to do it manually.
                  this is the script I use in my netbook- it may vary from device to device:

                  #!/bin/sh
                  #script to turn off internal display and use only the external VGA monitor
                  detect_external_vga=$(xrandr --listmonitors | grep 'VGA1' | awk {'print$4'})
                  if [ -n "$detect_external_vga" ]; then
                  xrandr --output LVDS1 --off --output VGA1 --mode 1440x900 --pos 0x0 --rotate normal
                  fi

                  – You can edit config files, so any system you use has the same settings, without having to change the config files manually or copying them manually, etc.- I do that in my ft10-transformation package

                  – You can automate almost anything you can do using a GUI, making it an automatic procedure.

                  – Did you know that, currently, the GUI that allows you to set time and date is a bash script, that uses yad for it’s interface? The same applies to “antiX Updater”, IceWM’s Toolbar Icon Manager (that allows to add/remove/move toolbar icons, using a GUI)… So, you probably have been using scripts, even if you don’t know it.
                  Tip: are you curious to see the contents of a script and check out how it works?
                  The “whereis” command helps you find where the executable file for a certain command is. If you are curious about how “antiX Updater” works, you can do this. I know the command to run it is “yad-updater”. If I want to know where the command is, so I can check it out, I could do this:

                  whereis yad-updater

                  The answer will be something like this:

                  yad-updater: /usr/local/bin/yad-updater

                  … this means that the command yad-updater is stored in /usr/local/bin/yad-updater

                  …so you can read it quickly by doing this: highlight the path of the file with your mouse, right click it, select “copy” and then type geany (don’t forget to always add a space after a command) and right click and paste the path to the file. Press enter. There you go, using a mix of the terminal and the GUI, you now can see how that script works… It uses variables, “if then”, the “touch” command, yad, and something I didn’t explain “Do – done” loops. Yeah… I sometimes have problems with “do”… scripting can get quite complex.

                  Final tip for creating scripts (with basic GUI’s and that open a terminal window, when needed):
                  Learning this was an eye opener and very usefull – let’s say you want to run a script that requires some terminal input/ouput (like, say, installing a package or updating your system), you may need to open a terminal so the user can see what is being done and answer any question the terminal asks…

                  If the terminal command requires elevated privileges (sudo) then you can use antix’s GUI way to elevate privileges: “gksu”, instead of sudo, in your script:

                  ex:gksu "apt update"

                  A small trick I use is running gksu on a phantom command, so that the user only sees what I want on the GUI, and not the ugly “apt update” command, and then, since the user already has elevated privileges, because of entering the root password in the gksu window, the script can do it’s job with the “sudo” command.

                  Force the system for forget any root password you just entered with this command: sudo -k
                  and run:

                   gksu "System updates Checker"
                   sudo apt update

                  Better looking, hum?

                  Now say that you want the user to just click the script from the file manager, without the need for opening the terminal (and get the terminal to open automaticaly when required, reducing the need to interact with it):

                  You can run any command you need in the new Terminal window (meaning you can start the script from anywhere- a file manager, from the menu, etc) like this:

                  x-terminal-emulator -T “[Title_of_the_terminal_window]” -e /bin/bash -c “[command(s)_that_the_script_as_to_run]

                  As soon as what’s done inside the terminal is finished, that Terminal window, that opened automatically, also closes automatically. This means that, if you use that window to run a command that takes a fraction of a second to run, all you’ll see is a Terminal window, flashing open and then instantly closing, in the same second. This should be used only in cases where the command that the script is running needs input from the user or the user should be able to read information that is displayed in the terminal. If you want the user to have some feedback, that all went well, you can use a simple yad window to convey that information.

                  Example of a simple GUI to update the system (this is based in the first generation of the antiX Updater script):

                    gksu "System updater"
                     x-terminal-emulator -T "System Updater" -e /bin/bash -c "sudo apt update && sudo apt dist-upgrade -V && sleep 0.1 && yad --center --width=250 --text-align=center  --text="Finished" --button='OK':1 --title=$'System updater'"

                  I call this kinds of scripts, that reduce the need for the user to interact with the terminal, “semi-GUI”- the user sees the terminal, but does not have to run any commands. I think they are a nice middle ground between using the Terminal only and using a fully GUI application (that is, to me, considerably more complex to write)

                  This is the sum of a nice chunk of some 6 years learning the Terminal and scripting, and I hope someone finds this useful… I sure wished I had a “crash course” like this when I came to Debian based distros, antiX in particular…

                  P.

                  • This reply was modified 1 month, 3 weeks ago by PPC.
                  • This reply was modified 1 month, 3 weeks ago by PPC.
                  • This reply was modified 1 month, 3 weeks ago by PPC.
                Viewing 7 posts - 61 through 67 (of 67 total)
                • You must be logged in to reply to this topic.