# Linux commands A shell is a program that receives commands from the user and gives it to the OS to process, and it shows the output. #### pwd It gives us the absolute path, which means the path that starts from the root. The root is the base of the Linux file system. It is denoted by a forward slash `/` ```shell= Air-Alena:~ alenaagapova$ pwd /Users/alenaagapova ``` #### ls and ls-a ls — Use the "ls" command to know what files are in the directory you are in. You can see all the hidden files by using the command “ls -a”. ```shell= Air-Alena:~ alenaagapova$ ls Alena Borodina - QA Engineer.pages Pictures Applications Public Book.txt PycharmProjects Desktop TestProject Documents User.txt Air-Alena:~ alenaagapova$ ls -a . Alena Borodina - QA Engineer.pages .. Applications .3T Book.txt .CFUserTextEncoding Desktop .DS_Store Documents .Trash Downloads .Xauthority Dropbox .android IdeaProjects .bash_history Library .bash_profile Movie.txt .bash_sessions Movies .cache Music .config Pictures .docker Public .dropbox PycharmProjects ``` #### cd Use the "cd" command to go to a directory. For example, if you are in the home folder, and you want to go to the downloads folder, then you can type in `cd Downloads`. Remember, this command is case sensitive, and you have to type in the name of the folder exactly as it is. If you have spaces in the name of the Dirctory use backslash `cd Raspberry\ Pi`. If you just type `cd` and press enter, it takes you to the home directory. To go back from a folder to the folder before that, you can type `cd ..` . The two dots represent back. ```shell= Air-Alena:~ alenaagapova$ cd Music Air-Alena:Music alenaagapova$ Air-Alena:Music alenaagapova$ cd .. Air-Alena:~ alenaagapova$ ``` #### mkdir & rmdir Use the mkdir command when you need to create a folder or a directory. For example, if you want to make a directory called “DIY”, then you can type `mkdir DIY` Use rmdir to delete a directory. But **rmdir can only be used to delete an empty directory**. To delete a directory containing files, use rm. ```shell= Air-Alena:NEW Dir alenaagapova$ mkdir LOL Air-Alena:NEW Dir alenaagapova$ ls LOL Air-Alena:NEW Dir alenaagapova$ rmdir LOL Air-Alena:NEW Dir alenaagapova$ ls Air-Alena:NEW Dir alenaagapova$ ``` #### touch The touch command is used to create a file. It can be anything, from an empty txt file to an empty zip file. For example, “touch new.txt”. ```shell Air-Alena:NEW Dir alenaagapova$ touch text.txt Air-Alena:NEW Dir alenaagapova$ ls text.txt ``` #### cp Use the cp command to copy files through the command line. It takes two arguments: The first is the location of the file to be copied, the second is where to copy. ```shell= /** copy to alenaagapova Air-Alena:NEW Dir alenaagapova$ cp text.txt /Users/alenaagapova /** check that file is still in current dir Air-Alena:NEW Dir alenaagapova$ ls text.txt /** go to alenaagapova dir Air-Alena:NEW Dir alenaagapova$ cd .. /** check that file is there too Air-Alena:~ alenaagapova$ ls Alena Borodina - QA Engineer.pages Pictures NEW Dir text.txt ``` #### mv Use the mv command to move files through the command line. We can also use the mv command to rename a file. For example, if we want to rename the file “text” to “new”, we can use “mv text new”. It takes the two arguments, just like the cp command. ```shell= Air-Alena:NEW Dir alenaagapova$ touch yes.txt Air-Alena:NEW Dir alenaagapova$ mv yes.txt no.txt Air-Alena:NEW Dir alenaagapova$ ls no.txt text.txt ``` Moving file to another directory: ```shell= /** move to alenaagapova dir Air-Alena:NEW Dir alenaagapova$ mv no.txt /Users/alenaagapova/ /** check that file is not in this dir anymore Air-Alena:NEW Dir alenaagapova$ ls text.txt /** check tha file is in alenaagapova dir now Air-Alena:NEW Dir alenaagapova$ cd .. Air-Alena:~ alenaagapova$ ls Alena Borodina - QA Engineer.pages Public Movies no.txt ``` #### locate The locate command is used to locate a file in a Linux system, just like the search command in Windows. Using the -i argument with the command helps to ignore the case. If you remember two words, you can separate them using an asterisk (*). For example, to locate a file containing the words "hello" and "this", you can use the command `locate -i *hello*this`. ```shell= /** locate considerinf case: Air-Alena:~ alenaagapova$ locate no.txt /Users/alenaagapova/no.txt /** nothing is found because of uppercase: Air-Alena:~ alenaagapova$ locate NO.txt /** locate ignoring case: Air-Alena:~ alenaagapova$ locate -i NO.txt /Users/alenaagapova/no.txt /** locate a file ending with 'ext.txt': Air-Alena:~ alenaagapova$ locate -i *ext.txt /Users/alenaagapova/NEW Dir/text.txt /Users/alenaagapova/text.txt ``` #### cat Use the cat command to display the contents of a file. It is usually used to easily view programs. ```shell= Air-Alena:~ alenaagapova$ cat text.txt hello. This is my text file. I want to tell you that you are my best friend. Ciao! ``` #### nano, vi nano and vi are already installed text editors in the Linux command line. vi is simpler than nano. You can create a new file or modify a file using this editor. For example, if you need to make a new file named "check.txt", you can create it by using the command “nano check.txt”. You can save your files after editing by using the sequence Ctrl+X, then Y (or N for no). ```shell= Air-Alena:~ alenaagapova$ nano text.txt ``` #### sudo A widely used command in the Linux command line, sudo stands for "SuperUser Do". So, if you want any command to be done with administrative or root privileges, you can use the sudo command. #### chmod Use chmod to make a file executable and to change the permissions granted to it in Linux. Imagine you have a python code named numbers.py in your computer. You'll need to run “python numbers.py” every time you need to run it. Instead of that, when you make it executable, you'll just need to run “numbers.py” in the terminal to run the file. To make a file executable, you can use the command “chmod +x numbers.py” in this case. You can use “chmod 755 numbers.py” to give it root permissions or “sudo chmod +x numbers.py” for root executable. #### hostname Use hostname to know your name in your host or network. Basically, it displays your hostname and IP address. Just typing “hostname” gives the output. ```shell= Air-Alena:~ alenaagapova$ hostname Air-Alena.fritz.box ``` #### ping Use ping to check your connection to a server. Simply, when you type in, for example, `ping google.com`, it checks if it can connect to the server and come back. It measures this round-trip time and gives you the details about it. ```shell= Air-Alena:~ alenaagapova$ ping google.com PING google.com (172.217.23.78): 56 data bytes 64 bytes from 172.217.23.78: icmp_seq=0 ttl=54 time=106.487 ms 64 bytes from 172.217.23.78: icmp_seq=1 ttl=54 time=98.749 ms ``` ## Tips and Tricks for Using Linux Command Line - You can use the `clear` command to clear the terminal if it gets filled up with too many commands. - `TAB` can be used to fill up in terminal. For example, You just need to type `cd Doc` and then TAB and the terminal fills the rest up and makes it `cd Documents`. - `Ctrl+C` can be used to stop any command in terminal safely. If it doesn't stop with that, then `Ctrl+Z` can be used to force stop it. - You can exit from the terminal by using the exit command. - You can power off or reboot the computer by using the command sudo halt and sudo reboot.