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.
1 2 3 4 |
# ls 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.
1 2 3 4 |
# 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.
1 2 3 4 |
# 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.
1 2 3 |
# pwd /home/raj |
/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.
1 |
# 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.
1 |
# cd ../ |
Linux File Permission Basics .
Touch Command – Create New Files in Linux
Creating new file is very simple in Linux using touch command. You have to specify only filename.
1 |
# touch newfile |
Now newfile is created, you can verify by typing ls command.
Touch Command – Complete Guide.
Create multiple files using touch command.
1 |
# 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 .
1 |
# 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.
1 |
# copy dir1 dir2 |
# Correct way of copying directory .
1 |
# 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.
1 |
# 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.
1 |
# mv /home/raj/Document/test.php /home/raj/Desktop |
Rename dir1 to dir2.
1 |
# 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.
1 2 3 |
# find /home/raj/Desktop -name "test.php" ./test.php |
Find file in current directory.
1 |
# 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.
1 2 3 4 |
# find . -iname "test.php" ./test.php ./Test.php |
Find all files whose file extension is .php .
1 2 3 4 5 6 7 8 |
# find . -name "*.php" ./AMQPStreamConnection.php ./AMQPSSLConnection.php ./AMQPConnection.php ./AbstractConnection.php ./AMQPSocketConnection.php ./AMQPLazyConnection.php |
Similarly you can find all files whose extension is .txt , .doc, .pdf etc.
Conclusion
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.