Command line cheatsheets

Here is the list of my general command line commands that I use in my experience.

Working with directories

Display path to current working place.

pwd

Go to a folder_name directory.

cd folder_name

Go to parent directory.

cd ..

Formatted list of files and folders in a directory.
-l - shows all files and folders like a list

ls -l

Formatted list with all files and folders including hidden types of files.
-l - shows all files and folders like a list
-a - shows all hidden files and folders

ls -la

Created the new directory with name folder_name.

mkdir folder_name

Working with files

Delete the file file_name.

rm file_name

Delete the directory folder_name with its subdirectories and files.
-r - delete all directories recursively
-f - force-delete files

rm -rf folder_name

Rename the file from old_file_name to new_old_file_name.

mv old_file_name new_old_file_name

Copy the file file_name to the directory folder_name.

cp file_name folder_name

Copy the directory source_folder_name with its subdirectories to the directory target_folder_name.
-r - copy a directory including all its content

cp -r source_folder_name target_folder_name

Clean a big log file path_to_log_file

cp /dev/null path_to_log_file

Working with permissions

Change permission of the file file_name to code 755

chmod 755 file_name

Change permission of the directory folder_name to code 755 including its subdirectories.
-R - change permission recursively for all subdirectories

chmod -R 755 folder_name

Change ownership of the file file_name to user user_name and to the group group_name

chown user_name:group_name file_name

Change ownership of the directory folder_name to user user_name and to the group group_name
-R - change ownership recursively for all subdirectories

chown -R user_name:group_name folder_name

Working with outputs

Clear command line interface.

clear

Show the content of the file with the name file_name.

cat file_name

Check the information of disk usage of files and directories on a system.
-s - display only total space
-h - display in human-readable format

# Check space in root directory
du -sh /* 
# Check space in 'some_folder' directory
du -sh /some_folder/*

Working with processes

Kill process with the id process_id

kill process_id

Kill a process on the port port_value (example: 8080)

sudo kill -9 $(sudo lsof -t -i:port_value)
# or
sudo fuser -k port_value/tcp

Working with services

Restart Apache

sudo service apache2 restart

Restart MySQL

sudo /etc/init.d/mysql restart