Basic UNIX Commands

1. How to create new user from command line?

      We need to use "useradd" command, which is responsible for creating a new user or update default new user information.

$ useradd <<user_name>>

      The new user will be entered into system files (/etc/passwd and /etc/shadow) as needed, the home directory (/home/user_name) will be created and initial files copied depending on the command line options.

By default user account is locked, we need to setup a new password.

$ passwd <<password>>

2. How to delete the user from command line?

$ userdel <<user_name>>

3. Commands to know logged in users?

$ w
$ who
$ users         // to know users logged in
$ whoami    // to know logged in user name

4. How to get the user activation details?

$ finger <<user_name>>
$ finger –s <<user_name1>> ...  <<user_name_n>>
$ finger –l <<user_name1>> ...  <<user_name_n>>

5. Which command is used to know linux O.S version/System identification details?

$ uname -a

And also check for $ cat /etc/redhat-release command – To get red hat version

6. How to check the assigned IP address of a server?

$ ifconfig

7. How to find the server uptime?

$ uptime

8. How to know the particular command usage?

$ man <<command_name>>
$ man uname

9. How to get virtual memory statistics like processes, memory and CPU activity?

$ vmstat
$ vmstat –s

And also check for

$ iostat
$ mpstat

10. Command to get the free and used memory?

$ free –m    -- To get the details in MB
$ free –g     -- To get the details in GB

11. How to get the CPU info from command line?

$ lscpu

12. How to find disk info (used space, free space and mounted space list)?

$ df –h

13. How do we get the size of a directory on the command line?

$ du –ch <<dir_name>> | grep total
$ du –ch <<dir_name>>
$ du –sh *

$ ls –ltrh
$ ls –ltrh | grep total

14. How to create, rename and remove a particular directory?

$ mkdir <<dir_name>>    -- To create directory in current location

$ mkdir /tmp/<<dir_name>>    -- To create directory in absolute path (inside tmp directory)

$ mkdir <<dir_name1>> <<dir_name2>>    -- To create multiple directories at a time

$ rm <<old_dir_name>> <<new_dir_name>>    -- To rename directory

$ rmdir <<dir_name>>    -- To remove directory

$ rmdir <<dir_name1>> <<dir_name2>>    -- To remove directories at a time

15. How do we navigate between directories in terminal?

$ cd ..    // To move directory to one level above

$ cd ~   // To navigate to home directory

$cd      // To navigate to home directory

$cd -    // To navigate to previous directory

16. How to know the present working directory?

$ pwd

17. List of commands which we usually perform on files in UNIX?

$ vi <<file_name>>    -- To create new file (or) to edit file

$ cat <<file_name>>    -- To view the content of the file

$ cat <<file_name>> -b    -- To view file content with row numbers

$ wc <<file_name_1>> …  <<file_name_n>>    -- To count words in a file(s)

$ cp <<source_file>> <<dest_file>>    -- To copy file

$ mv <<old_file>> <<new_file>>    -- To rename file

$ rm <<file_1>> …  <<file_n>>     -- To remove file(s)

18. How to replace a particular string with new one in a file?

Open the file in vi editor and follow the below command


And save the file with “:wq!” command

19. How to sort the file content alphabetically (or) numerically?

$ sort <<file_name>>

20. How to print particular column data from a file?

$ awk ‘{print $2,$3;}’ employee.txt    -– to print columns 2 and 3 from employee.txt file

21. How to truncate the file without disrupting the processes?

$ truncate -s 0 <<file_name>>
$ echo “” > <<file_name>>

22. How to give permissions to a file?

$ chmod 755 <<file_name>>    - (set permissions for ugo -->  User, Group and Others)

$ chown <<user_name/uid>> <<file_name>>     -- to change owner of the file

$ chown <<group_name/gid>> <<file_name>>     -- to change group of the file

23. Basic ‘ls’ commands in linux?

$ ls    –- to list files and directories in current directory

$ ls –l    -- to list files and directories with full details

$ ls –a    -- to list hidden files also

$ ls /root/tmp     --to list files in absolute path (/root/tmp)

$ls –lah –    to know the size of a file/particular directory

$ ls –la | grep –i “abc”    -- Lists all with matching pattern in upper or lower case

$ ls –la | grep –c “abc”    – c -->  counts all with matching pattern

24. How to view a large file in Linux?

$ cat <<file_name>> | more

and type,

s -->  for next page
d -->  for next 25 lines of text

25. How to move file/directory from one linux box to another?

$ scp <<local_file_path>> <<user_name>>@<<machine_name>>:<<remote_file_path>>    -- To move file from one linux machine to other

$ scp –r <<local_directory_path>> <<user_name>>@<<machine_name>>:<<remote_directory_path>>    -- To move directory from one linux machine to other

26. How to use tar command to compress and decompress a directory?

$ tar cvf archive_name.tar dirname/     -- To create an uncompressed tar archive

$ tar xvf archive_name.tar     -- To extract a tar file

27. How to get list of running processes?

$ ps –f    -- Lists running processes

$ ps –ef    -- Lists running processes ( provides extended information )

$ ps –ef | grep “<<App_Name>>” -- to get specific process is up and running or not (here it is App_Name)

$ top    -- Shows running processes sorted by various criteria

$ top –b –n 1 | head –n 5    -- To get only head info of top command output

28. How to do you stop the running processes?

First you should get the PID by running above command i.e. $ ps –ef | grep “<<App_Name>>” and then run below commands

$ kill <<PID>>
$ kill -9 <<PID>>

29. How to get Foreground and background processes?

$ jobs    -- Lists the background processes with job ID

$ jobs –l    -- Lists the background processes with process IDs

$ jobs –p    -- To get only process IDs

$ fg <<Job_ID>> (or) $ %Job_ID    -- To bring process running in background to foreground

$ bg <<Job_ID>> (or) $ %Job_ID &    -- To bring process running in foreground to background

30. How to check if a particular process is listening on a particular port on remote host?

$ telnet <<host_name>> <<port>>

31. How to check the status of a machine in the same network?

$ ping <<host_name>> (or) <<ip_address>>

32. How to get the IP address of a domain (or vice versa)?

$ nslookup <<domain_name>>

33. How to know the port availability in linux machine?

$ lsof –i :<<port>>

$ netstat –al | grep –i <<listen_port>>

34. How to clear the swap memory?

$ sudo swapoff –a
$ sudo swapon –a

35. How do we create a soft link/symbolic link in Linux?

$ ln –s <<target_filename>> <<symbolic_filename>>

36. How to search single/multiple files for some string value?

$ find . –name “<<file_name>>” –exec grep –i “<<searh_string>>” ‘{}’ \; -print    -- To search in single file

$ find . –name *.* –exec grep –i “<<searh_string>>” ‘{}’ \; -print    -- To search in multiple files

   Commands which are provided above all are of our knowledge, Please let us know if you find any change in command and also update us for any new commands and same will be shared here with external world. Have A Nice Day !!! Thank You !!!

No comments:

Post a Comment

back to top