Basic Linux commands
May 9, 2021 • 8 minutes read

Basic Linux commands

Navigation

Command Description
cd /var/www go to the /var/www folder
cd .. go to the folder one level above
cd ../.. go on 2 levels higher than the current directory
cd go to the home folder
cd ~john go to the john's home folder
cd - go to the directory in which you were before switching to the current directory
pwd show the path to the current directory
ls display the contents of the current directory
ls -lh print a detailed list of files and directories in the current directory
ls -a show hidden files and folders in the current directory

Managing Files and Folders

Сommand Description
mkdir dir1 create a directory with the name dir1
mkdir dir1 dir2 create 2 directories with the names dir1 and dir2
mkdir -p /tmp/dir1/dir2 create directories recursively
rm -f file1 remove file with the name file1
rmdir dir1 remove directory with the name dir1
rm -rf dir1 delete the directory named dir1 and recursively all its contents
rm -rf dir1 dir2 delete two directories and recursively their contents
mv dir1 new_dir rename or move a file or directory
cp file1 file2 copy file file1 to file file2
cp dir/* . copy all files in the dir directory to the current directory
cp -a /tmp/dir1 . copy directory dir1 with all contents to the current directory
cp -a dir1 dir2 copy directory dir1 to directory dir2
ln -s file1 lnk1 create a symlink to a file or directory
ln file1 lnk1 create a "hard" link to a file or directory

Text files

Command Description
cat filename.txt show content from filename.txt
tail /var/log/syslog -n 5 show last 5 records from the end of the file
head /var/log/syslog -n 5 show first 5 records from the begging of the file
nano filename.txt edit filename.txt
touch -t 0712250000 filename.txt modify the creation date and time of an existing file or create a file with the specified date and time (YYMMDDhhmm)

Permissions

Permissions represented as numbers. Each number shows access level

Number Permission Text representation
0 No permission granted ---
1 Can execute --x
2 Can write -w-
3 Can write and execute (2 + 1 = 3) -wx
4 Can read r--
5 Can read and execute (4 +1 = 5) r-x
6 Can read and write (4 + 2 = 6) rw-
7 Can read and write and execute (4 + 2 + 1 = 7) rwx

The various owners can be represented as one symbol in Linux

Symbol Description
u user (owner)
g group
o other
a all
Command Description
chmod g+w file.txt add write access for group
chmod g-w file.txt remove write access for group
chmod +w file.txt add write access for all
chmod g+x test.sh add execute access for group
chmod 000 secret-file.txt no permissions granted for all
chmod 754 test.sh Owner can read and write and execute. Group can read and execute. Others can read only
chmod 777 /var/test/ -Rf Add full permissions for all recursively

Script and program execution

Command Description
./script.sh run script.sh from current user from current directory
sudo ./script.sh run script from root user
sudo -u www-data whoami run command whoami from www-data user
sudo -i run another terminal session from root user

Process control

Сommand Description
ps -auxf display running processes
pstree display the process tree
kill -9 98989 or kill -KILL 98989 "kill" the process with PID 98989 "to death" (without observing data integrity)
kill -TERM 98989 correctly terminate the process with PID 98989 by sending a TERM signal to it
kill -1 98989 or kill -HUP 98989 send a signal to re-read the configuration file to the process with PID 98989
Last update March 18, 2023
Cheat Sheet linux
Translation of this article: Русский, Українська
Do you have any questions?

Contact Me