Search code snippets, questions, articles...

30 Terminal Commands that every programmer must know

If you are a programmer, you must be using terminal commands on MAC OS and cmd on windows to do specific tasks quickly. In this post, we are going to explain the commands that will extend your knowledge of useful commands.

Lets's start with the basic command that can be used to perform specific tasks in the directory or folders created on your system.

cd command

The 'cd' command can be used to go to a particular directory or folder that exist on your system. For example if you want to go to the Desktop folder of your system, you can use the below command in the terminal.

cd ~/Desktop

pwd command

The pwd command is used to print the current directory path in your terminal. Just write the pwd command in the terminal and press enter and it will print the director path in the terminal.

pwd

Output

/Users/name/Desktop

ls command

The 'ls' command will show all the content that a directory or folder has. It will print the names of all directories and files inside the directory from where you ran the command.

ls

Output

DB-Backups	        Projects
image123.png		icon.png
pdf_file1.pdf		logo.png

clear or cls command

If you have printed a lot of content in the terminal and want to clear that, you can run the command 'clear' or 'cls' in the terminal.

clear

or

cls

cp command

The 'cp' command is used to copy the files from one folder to another. We need to specify the path of the source file and destination folder in the command. 

Syntax

cp source_file_path destination_path

Command example

cp ~/Desktop/source/hello.rtf ~/Desktop/destination

touch command

The touch command can be used to create a blank file. For example, if you want to create a text file - test.txt then you can use the below command and it will create the test.txt file in the current directory.

touch test.txt

mv command

If you want to cut and paste a file from one directory to another directory then you can use the 'mv' command. You need to specify the source file path and destination path to move the file.

Syntax

mv source_file_path destination_folder_path

mv command example

mv ~/Desktop/source/hello.rtf ~/Desktop/destination

mkdir command

The mkdir command will create a new directory inside the folder you are executing the command.

mkdir directory_name

If you want to create a folder - test using mkdir command, you can use

mkdir test

rmdir command

The rmdir command is used to remove an empty directory from the current directory. You need to specify the directory name and it should be empty to remove via rmdir command.

rmdir test_dir

If test_dir directory is not empty it will not delete the directory and show - Directory not empty message in the terminal.

rm command

The rm command can be used to delete files from a directory. For example, if there is file info.txt and you want to delete it using the terminal command then you can use the below command.

rm info.txt

To delete a directory and all of its files and directories inside it, you can use the rm -r directory_name terminal command.

rm -r directory_name

find command

The find command can be used to find a file or directory in the current directory. You need to specify the file name that you want to search in the given directory. For example, if you want to search a file named readme.md then you can run the below command in the terminal.

find readme.md

grep command

The grep command is used to find a text in all the files that exist inside the current directory. For example, if you want to find a text - 'things' in all the files of a specific directory then you can use below command.

grep -R 'things' .

Output

./data_files/info2.txt:Can make things good
./data_files/info1.txt:All the things

df command

The df command can be used to print the report of disk usage on the system. The disk usage data will be printed in the terminal when you run the df command.

df

Output

Filesystem    512-blocks      Used Available Capacity iused      ifree %iused  Mounted on
/dev/disk1s1   489620264  21652648 276028680     8%  488254 2447613066    0%   /
devfs                387       387         0   100%     672          0  100%   /dev
/dev/disk1s6   489620264 173697784 276028680    39% 1084115 2447017205    0%   /System/Volumes/Data
/dev/disk1s4   489620264  16777296 276028680     6%       8 2448101312    0%   /private/var/vm
map auto_home          0         0         0   100%       0          0  100%   /System/Volumes/Data/home

du command

The du command is used to get the information about the disk usage that the files and directories of the current directory are using.

du

Output

0	./hello2
16	./data_files
32	.

head command

The head command is used to print the first ten lines of a file in the terminal. For example, if you have a text file info.txt and you want to print the first ten lines of it then you can use the below command.

head info.txt

Output

Hello
This
Is
Text file
That 
You 
Can make things good
And
We can 
Use the files

tail command

The tail command is used to print the last ten lines of a text file in the terminal. For example, if you have a text file - info.txt and you want to print the last ten lines of it in the terminal then you can use the below command.

tail info2.txt
To make 
It more
Quick 
And easy
This will gives 
You 
An idea 
About 
The current
structure% 

diff command

The diff command is used to compare the text of two files line by line. For example, if you have two text files info1.txt and info2.txt and you want to compare the text of these files using the terminal then you can use the below command.

diff info1.txt info2.txt

tar command

The tar command is used to archive the files or full directory using the terminal command.

Syntax

tar -czvf archive_name.tar.gz /path/to/directory-or-file

Example

If you have a folder - data_files that contains multiple files and folders and you want to create an archive file using the tar command then you can use the below command.

tar -czvf data-files.tar.gz data_files 

jobs command

The jobs command can be used to display the jobs that are running on your system along with their status.

jobs

ping command

The ping command is used to check the connection to the server from your system. Form example, if you want to check the connectivity of a website that has the domain name - domain.com then you can use the below command for that.

ping domain.com

ls [email protected] command

The ls [email protected] command can be used to view the permissions assigned to a file. For example, if you have a file info.txt and you want to check the permissions assigned to it then you can use the below command.

 ls [email protected] info.txt

Output

[email protected] 1 username  staff  41 Mar 11 21:48 info.txt

chmod command

The chmod command can be used to change the read and write permissions on a file for different users or groups. For example, if you have a file info.txt and you want to change the permission on that you can use the below command.

 chmod u=rw,g=r,o=rw info.txt

This will set

u=rw set read and write permission for the file owner.

g=r sets read permission for user groups.

o=rw sets read and write permissions for everyone.

Was this helpful?
0 Comments
Programming Feeds
Learn something new everyday on Devsheet