Main GNU/Linux commands to know when you're a beginner
I will put here the main commands to know in a GNU/Linux environment when you start with it.
PWD - Where am I?
The pwd command
To know in which path you are, you can use the pwd command.
user01@host:~$ pwd /home/user01
Here, we can see that we are inside the user01 home directory.
Linux Directory Structure
Remember the Linux path structure (thanks to Wikipedia) and its Windows OS equivalence.
| Directory | Description | Windows equivalence |
|---|---|---|
| / | root directory of the entire file system hierarchy. | C:\ |
| /bin, /sbin | command binaries. | C:\Program Files |
| /boot | Boot loader files. | |
| /dev | Device files. | Device Manager |
| /etc | System configuration files. | Windows Registry |
| /home | Users' home directories. | C:\Users |
| /root | Home directory for the root user. | C:\Users\Administrator |
| /tmp | Directory for temporary files. | C:\Windows\Temp |
| /var | Variable files: files whose content is expected to continually change during normal operation of the system. |
CD - Change directory
Change working directory, we can use the cd (change directory) command.
- Going to /tmp directory :
user@host:~$ cd /tmp
CP - Copy a file or a directory
Files
To copy a file or a directory we can use cp.
- Copy my_file to my_file.bak :
user@host:~$ cp my_file my_file.bak
- Copy my_file to a different directory :
user@host:~$ cp my_file /tmp/
- Copy multiple files to a directory :
user@host:~$ cp my_file my_file.bak -t /tmp/
Directories
- Copy a directory (-r : recursively) :
user@host:~$ cp -r my_directory my_directory.bak
MKDIR - Create new directory
- Create a directory :
user@host:~$ mkdir my_directory
MV - Move a file or a directory
Files
To move a file or a directory we can use mv.
- Move my_file to my_file.bak :
user@host:~$ mv my_file my_file.bak
- Move my_file to a different directory :
user@host:~$ mv my_file /tmp/
- Move multiple files to a directory :
user@host:~$ mv my_file my_file.bak -t /tmp/
Directories
- Move a directory :
user@host:~$ mv my_directory my_directory.bak
LS - Show files and directories
To list files we can use ls.
- List directories and files from current location :
user@host:~$ ls my_directory my_directory.bak my_file my_file.bak
- List directories and files from current location with details informations (size, owners, right etc..) :
user@host:~$ ls -l total 8192 drwxr-xr-x 2 user user 40 25 oct. 12:12 my_directory drwxr-xr-x 2 user user 40 25 oct. 12:12 my_directory.bak -rw-r--r-- 1 user user 8388608 25 oct. 12:12 my_file -rw-r--r-- 1 user user 0 25 oct. 12:12 my_file.bak
You can see the d indicator for directories.
- We can add -h option to have human readable sizes :
user@host:~$ ls -lh
total 8,0M
drwxr-xr-x 2 user user 40 25 oct. 12:12 my_directory
drwxr-xr-x 2 user user 40 25 oct. 12:12 my_directory.bak
-rw-r--r-- 1 user user 8,0M 25 oct. 13:22 my_file
-rw-r--r-- 1 user user 0 25 oct. 12:12 my_file.bak
RM - Remove files or directory
To remove files or directories we can use the rm command.
Be aware that there is no Recycle Bin, so once you remove files or directories, they'll be forever lost.
Files
- Here we remove the my_file file :
user@host:~$ rm my_file
Directories
- To remove directories we use the -r (for recursive) argument :
user@host:~$ rm -r my_directory
TAIL - Output the last part of files
Particulary useful when you want to watch real time logs.
user@host:~$ tail -f /var/log/syslog
FIND - Find files or directories
- Example here if we want to search inside /home/user for every files and folders which include "std" in their names :
user@host:~$ find /home/user -iname '*std*' /home/user/std /home/user/anything_std_want /home/user/easy_std_way /home/user/easy_std /home/user/anemia_std