In this article, you will learn some of the most used Linux commands with examples . If you are new to Linux or Ubuntu operating system then all these commands are very helpful for you.
Linux Commands with Examples
To start with following commands open the terminal (ctr + Alt + T) and start playing.
LS command – List all files and directories in Linux
ls command is used to list all files and directories in current directory.
Desktop login_form.tpl test_project
Documents Music test.txt
If you want to check file type, modified date, owner of file and it’s permission, use -l switch with ls.
# ls -l
drwxr-xr-x 11 raj raj 4096 Jun 27 14:49 Desktop
drwxr-xr-x 3 raj raj 4096 May 24 15:49 Documents
drwxr-xr-x 10 raj raj 4096 Jun 28 08:53 Downloads
List hidden files using -a with ls.
# ls -a
.bashrc Desktop .tuts test_project
Documents .nautilus Music test.txt
PWD Command – Check Current Working Directory in Linux
pwd command is used to check current working directory.
/home/raj is my current directory, if i move to some other directory and then type pwd then it display other path.
CD Command – Change Current Directory in Linux
cd command is used to change current directory.
Move to Documents directory.
# cd /home/raj/Documents
Move one level up. Now my current directory is Documents (/home/raj/Documents). Now i want to go one level up on /home/raj directory.
# cd ../
Touch Command – Create New Files in Linux
Creating new file is very simple in Linux using touch command. You have to specify only filename.
# touch newfile
Now newfile is created, you can verify by typing ls command.
Create multiple files using touch command.
# touch file1 file2 file3
Created file1, file2 and file3.
CP Command – Copy Files and Directories in Linux
cp (copy) command is used to copy files and directories from one location to another. In cp command you have to specify source and destination.
Copy contents of config.php to config.php.bak .
# copy config.php config.php.bak
While copy directory -r switch is required. You cannot copy one directory to other by simply copy command.
# Wrong way of copying directory.
# copy dir1 dir2
# Correct way of copying directory .
# copy -r dir1 dir2
mkdir Command – How to Create Directory in Linux
mkdir command is used to create new directory.
Let’s create a new directory webapp.
# mkdir webapp
If you want to check whether the directory is created or not type ls command.
MV Command – move (rename) files
mv command is used to move files and directories from one place to another in Linux. You can also rename files and directories using this command.
Move test.php file from Document directory to Desktop.
# mv /home/raj/Document/test.php /home/raj/Desktop
Rename dir1 to dir2.
# mv dir1 dir2
FIND Command – Search files
find command search for files in a directory hierarchy.
Let’s say i have to find test.php file in Document directory.
# find /home/raj/Desktop -name "test.php"
Find file in current directory.
# find . -name filename
. (dot) represents current directory.
Find file by name ignoring case (case insensitive search) . In linux test.php and Test.php are two different files. If we want to ignore case then we need to use -i switch.
# find . -iname "test.php"
Find all files whose file extension is .php .
# find . -name "*.php"
Similarly you can find all files whose extension is .txt , .doc, .pdf etc.
I tried my best to mentioned all the most used Linux commands. If you want to add any command please let us know through your comments.