DEV Community

Cover image for Basic Linux/Unix Commands We should know
Payalsasmal
Payalsasmal

Posted on • Updated on

Basic Linux/Unix Commands We should know

Background
I was a window user, I never thought to use Linux in my life time. But Suddenly it changed my world and I don't want to go back to the windows because I fell in love with linux.
I am not a system admin now but I work on few Linux distribution which made me realized that whoever is going to learn Linux, they should know these basic Linux commands, which will help them to save few times and practice.

1. mkdir:

mkdir stands for make directory i.e. this command is responsible to create directories.

Syntax:

mkdir [FILENAME]
Enter fullscreen mode Exit fullscreen mode

Example:

Alt Text

2. ls:

ls stands for list i.e. list the names of files and directories in current working directory.

Example:

Alt Text

Syntax:

ls [OPTIONS]
Enter fullscreen mode Exit fullscreen mode

There are various options which support ls command. Listing few of them in below.

  • ls -a : list all the files including hidden files. even though it include "." and ".." files.

  • Example:

Alt Text

  • ls -A : list all the files including hidden files except "." and ".." files.

Example:

Alt Text

  • ls -r: list the files in descending order name excluding hidden files.

Example:

Alt Text

  • ls -R: list the files recursively and it shows the files inside directories.

Example:

Alt Text

  • ls -l: list the files in long format i.e. name of each file, owner name, group name, size, index number, timestamp, permission etc.

Example:

Alt Text

  • ls -g: list the files in long format but without owner name.

Example:

Alt Text

  • ls -o: list the files in long format but without group name.

Example:

Alt Text

  • ls -s: list the files with their sizes.

Example:

Alt Text

  • ls -S: sort the files with their sizes.

Example:

Alt Text

  • ls -i: list the files with their index numbers.

Example:

Alt Text

  • ls -t: sort the files with time, latest should be at top.

Example:

Alt Text

  • ls -p: append "/" to directories.

Example:

Alt Text

  • ls -d */: list the directories

Example:

Alt Text

  • ls -ltr: we can merge the above command together based on requirements. This is representing that list of all the files in long format, sort the files with modification time, in descending order.

Example:

Alt Text

3. cd:

cd full form is change directory. so it's used to change the shell directory.

Syntax:

cd [Directory Name]
Enter fullscreen mode Exit fullscreen mode

But while going back to the parent directory of current directory, need to use below commands

cd ..
Enter fullscreen mode Exit fullscreen mode

While going back to the parent's of parent's directory, need to use below commands.

cd ../..
Enter fullscreen mode Exit fullscreen mode

Based on the requirements we can add more ".." and "/" after cd command.

Example:

Alt Text

4. lsof:

lsof stands for List Of Open File i.e. this command provide the information of list of opened files and opened for which process.

Syntax:

lsof [OPTIONS][USER NAME]
Enter fullscreen mode Exit fullscreen mode

Example:

  • Provided $lsof -u [USER NAME] command to see the opened file for specific user.

Alt Text

5. watch:

It gives continuous output in full screen mode with 2.0s interval.

Syntax:

watch [command]
Enter fullscreen mode Exit fullscreen mode

Example:

Provided command watch date in below example

Alt Text

5. df:

df stands for disk free i.e. it is used to show information about file system's total space and available space.

Syntax:

df [OPTION]...[file]...
Enter fullscreen mode Exit fullscreen mode

Example:

  • If no file name is provided then it displays space available and used on all current mounted file systems.

Alt Text

  • If file name is provided then it display space available and used on that particular file.

Alt Text

6. who:

Provides the details of currently logged in user information.

  • Login name of the users
  • Terminal Line Number
  • Login Time of the user
  • Remote hostname of the user

Syntax:

who [OPTIONS]...[FILE]...
Enter fullscreen mode Exit fullscreen mode

Example:

Alt Text

7. wget:
Use to download the files from the server using HTTP,HTTPS,FTP protocols.

Syntax:

wget [OPTIONS]...[FILE]...
Enter fullscreen mode Exit fullscreen mode

Example:

Alt Text

8. w:
Use to see the information of user who is logged in and what they are doing.

The header provide the information of current time, how long system is running, how many users logged in and load average for past 1, 5 and 15 mins.
Other than these it display each user- login name, tty, remote host, login time, idle time, JCPU , PCPU and the command line of their current process.

Syntax:

w [OPTIONS] USER ...
Enter fullscreen mode Exit fullscreen mode

Example:

Alt Text

9. wc:

wc stands for word count i.e. it is used for counting purpose.
It counts the number of lines, number of words, number of characters.

Syntax:

wc [OPTIONS]... [FILE]...
Enter fullscreen mode Exit fullscreen mode

Example:

First column represent number of lines present in specific file, second column represent the number of words present in specific file, third column represent number of character presents and fourth column represents the name of the file.

Alt Text

10. lshw:

lshw stands for list of hardware i.e. it displays details of system's hardware information. To access this command we should be a super user.

Syntax:

lshw [FORMAT]... [OPTIONS]...
Enter fullscreen mode Exit fullscreen mode

Here format can be html, xml, json, short, businfo.

Example:

  • It provides the full hardware information.

Alt Text

  • It list the hardware info in compact format.

Alt Text

11. man:

man stands for Manual i.e. it is used to display user manual for any command.
It provides details of command which includes : NAME, SYNOPSIS, DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUES, NOTES, FILES, EXAMPLES, AUTHOR, REPORTING BUGS, COPYRIGHT and SEE ALSO.

Syntax:

man[OPTION]... [COMMAND NAME]...
Enter fullscreen mode Exit fullscreen mode

Example:

  • Provided $man lshw command without option and shown the whole manual of the specified command name.

Alt Text

  • Provided $man -w [COMMAND NAME] command with option and shown the location of the manual page for the specified command name.

Alt Text

12. cat:

cat stands for concatenate. It reads data from the files and give their output. It also helps us to create file, view files, concatenate files.

Syntax:

cat[OPTION]... [FILE]...
Enter fullscreen mode Exit fullscreen mode

Example:

  • Provided $cat [file_name] command to get the output whatever is inside the file.

Alt Text

  • Provided cat >[file_name] command to create the file and at the same time you can provide the text inside the file. After that you can exit using Ctrl+D.

Alt Text

13. rm:

rm stands for remove. remove commands can remove the files, directories etc. But by default it doesn't remove directories. But Be careful of these commands.

Syntax:

rm[OPTION]... [FILE]...
Enter fullscreen mode Exit fullscreen mode

Example:

  • Provided $rm [FILE NAME] command to remove specified file name.

Alt Text

  • $rm -r: remove directories recursively.

  • $rm -f: remove Read only file without permission.

  • $rm -rf. : Force deletion of Current folder and sub folders.

  • $rm -rf/ : Force deletion of Everything in root directory.

  • $rm -rf* : Force deletion of Everything in current directory.

  • $rm -rf [DIRECTORY NAME] : Force deletion of Specified Directory Name. It deletes everything whatever present inside of the directories.

  • $rmdir [DIRECTORY NAME]: Remove the Specified Directory Name. If directories content files or folders, this command unable to delete directories.

14. pwd:

pwd stands for Print Working Directory. It prints path of the working directory, starting from the root.

Syntax:

pwd [OPTION]
Enter fullscreen mode Exit fullscreen mode

Example:

Alt Text

15. mv:

mv stands for Move i.e. it moves file or files or directories from one place to another place.

Syntax:

mv [OPTION] [Source] [Destination]
Enter fullscreen mode Exit fullscreen mode

Example:

Alt Text

  • Provided $mv [file1] [file2] command, which is responsible to rename the file1 to file2 and override the file2 value with file1 value.

Alt Text

  • Provided $mv -b [file1] [file2] command, -b is responsible to take the backup of overridden file value(file2). It creates a backup file with tilde (~) character append with it.

Alt Text

NOTE: You can use --help option with any of the command to know more about the commands.

Alt Text

Top comments (8)

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great tutorial! Well done! 🙌

If you are an open-source enthusiast feel free to contribute to the Introduction to Bash Scripting eBook.

I think it would be beneficial for people to know those commands before they get started with Bash scripting.

Collapse
 
payalsasmal profile image
Payalsasmal

Thanks Bobby, I am really happy to hear this. I saw your ebook, It's very helpful. I would love to contribute on Bash Scripting eBook. I will read your contribution file thoroughly and I will make a PR soon. 😊

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Welcome to the light side of the force

Collapse
 
payalsasmal profile image
Payalsasmal

I really put a lot of thought into this, thank you for noticing. 🙂

Collapse
 
mccurcio profile image
Matt Curcio

Wow, that is quite a comprehensive list. Good Start!

Collapse
 
payalsasmal profile image
Payalsasmal

Thank you so much

Collapse
 
omidziveh profile image
omidziveh

very helpful
thanks

Collapse
 
payalsasmal profile image
Payalsasmal

Thanks for noticing. It is really great to hear that. 🙂