Jul 132015

UNIX Commands  


1. CAT: Concatenate files to standard output

The “cat” command can be used to:

  • Display text files
  • Copy text files into a new document
  • Append the contents of a text file to the end of another text file, combining them


cat [options] [file_names]


$ cat myfile.txt

This command will display the contents of the text file “myfile” on the screen.

$ cat myfile.txt > mynewfile.txt

This command will read the contents of myfile.txt and send them to standard output; instead of displaying the text, however, the shell will redirect the output to the file mynewfile.txt. If mynewfile.txt does not exist, it will be created. If mynewfile.txt already exists, it will be overwritten and its previous contents will be lost, so be careful.

$ cat myfile.txt >> mynewfile.txt

This command will read the contents of myfile.txt, and write them at the end of mynewfile.txt. If mynewfile.txt does not already exist, it will be created and the contents of myfile.txt will be written to the new file.

$ cat myfile1.txt myfile2.txt > mynewcombinedfile.txt

This command will concatenate 2 text files myfile1.txt and myfile2.txt and write them to a new file mynewcombinedfile.txt. $ cat –n myfile1.txt myfile2.txt > mynewcombinedfile.txt -n will number all output lines from file mynewcombinedfile.txt.

2. CD: Change directory

The “cd” command allows you to change your working directory. You use it to move around within the hierarchy of your file system.


$ cd


$ cd ~

These commands will put you in your home directory.

$ cd.

This command will leave you in the same directory you are currently in (i.e. your current directory won’t change).

$ cd ~username

This command will put you in username’s home directory.

$ cddir  (without a /)

This command will put you in a subdirectory. for example, if you are in /usr, typing cd bin will put you in /usr/bin, while cd /bin puts you in /bin.

$ cd ..

This command will move you up one directory. So, if you are /usr/bin/tmp, cd ..moves you to /usr/bin, while cd ../.. moves you to /usr (i.e. up two levels). You can use this indirection to access subdirectories too. So, from /usr/bin/tmp, you can use cd ../../local to go to /usr/local.

$ cd –

This command will switch you to the previous directory (UNIX only). For example, if you are in /usr/bin/tmp, and go to /etc., you can type cd – to go back to /usr/bin/tmp. You can use this to toggle back and forth between two directories.

3. CHMOD: Change access permissions

The “chmod” command is used to change the permissions of files or directories.


$ chmod u=rwx,g=rx,o=r file1

This command allows the owner to set the following permissions

  • The user can read, write, and execute the file
  • Members belonging to your group can read and execute the file
  • Others may only read the file.
  • u – user, g – group, o – other
  • r – read, w – write , x – execute

$chmod 754 file1

This command is equivalent to the command used above. The permissions for the user, group and others are represented by the digits 7, 5 and 4. Each digit is a combination of numbers 4, 2, 1 and 0

4 – “read”, 2 – “write”, 1 – “execute”, 0 – “no permission”

4. CHOWN: Change file owner and group

The “chown”command is used to change the owner and owning group of a file.


$ chown root /var/run/httpd.pid

Change the owner of /var/run/httpd.pid to ‘root’ (the standard name for the Superuser).

$ chownrob:developers strace.log

Change the owner of strace.log to ‘rob’ and the group identifier to ‘developers’.

$ chown -R foouser base

Change the ownership of base to the user foouser and make it recursive (-R)

$ chown -R newuser:newgroup .

Change the ownership to newuser and group to newgroup for all of the files and directories in current directory, and all subdirectories (recursively).

5. CHGRP: Changing group

The “chgrp” command is used for changing groupownership of a file or files.


$ chgrp hope file.txt

Change the owning group of the file file.txt to the group named hope.

$ chgrp -hR staff /office/files

Change the owning group of /office/files, and all subdirectories, to the group staff.

6.   CKSUM: Print CRC checksum and byte counts

The “cksum” command is used to checksum and counts the bytes in a file.


$ cksum myfile.txt

This command calculates the checksum and byte count of myfile.txt and output the values along with the filename.


3042891102 56 myfile.txt

Where “3042891102” represents the checksum value and “56” represents the file size of myfile.txt.

7.   CMP: Compare two files

The “cmp” command is used to compare two files of any type and writes the results to the standard output. By default, cmp is silent if the files are the same; if they differ, the byte and line number at which the first difference occurred is reported.


$cmp myfile1.txt myfile2.txt

This command will compare myfile1 to myfile2, reading each file byte-by-byte and comparing them until one of the byte pairs is not equal. When a difference is found, it will output the location in the file where the difference was found, and exit.


Myfile1.txt myfile2.txt differ: char 1001, line 99

8. CP: Copy one or more files to another location

This “cp” command is used for copying files and directories.


$ cp myfile1.txt myfile2.txt

Through this command myfile1.txt will be copied to myfile2.txt, where myfile1.txt is the source of copy operation and myfile2.txt is the destination.

The source and destination files may also reside in different directories.

For example:

$ cp /home/test/data/ myfile1.txt /home/test/backup/myfile1.txt

9. DU: Estimate file space usage

The “du” command is used to estimate file space usage—space used under a particular directory or files on a file system.


$ du -sk *

Where, (-s) is the Sum of directories (-k) is kilobytes

Output:  123546 directoryOne1896545 directoryTwo           $ du -sh * Where, (-s) is the Sum of directories in human-readable format and (-h) can be Byte, Kilobyte, Megabyte, Gigabyte, Terabyte or Petabyte. Output:

123M directoryOne

1.2G directoryTwo

$du -shc *.txt This command will report the size of each file in a human-readable format, and also displays a grand total.          6.0K    myfile1.txt

2.0K    myfile2.txt

10.0K   myfile3.txt

18.0K   total

10. DF: Display free disk space

The “df” Command is used to report the amount of available disk space being used by file systems.


$ df

This command will display all the file systems and their disk usage.

$ df –h

This command is same as above, but use “human readable” formatting.

$ dfmytest_dir

This command will display the amount of free space in the mytest_dir directory.

11. FILE: Determine type of file

The “file” command is used for recognizing the type of data contained in a computer file.


$ file *.txt

This command will list any files ending with extension .txt

12. FUSER: Identify process using file

The “fuser” command is used to show which processes are using a specified file, file system, or UNIX socket. fuser displays the PIDs of processes using the specified files or file systems.


$ fuser .

This command will display every process ID that is using the current directory (“./”).

13. LS: List information about file(s)

The “ls” command is used to list the files and directories stored in the current directory.

Syntax: $ ls

Ls –l command: lists your files in ‘long format’, which contains lots of useful information, e.g. the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified.

Syntax : $ ls –l

Ls –a command: List the current directory including hidden files. Hidden files start

with “.”

Syntax: $ ls -a

Ls –x command: This command displays the list of files/folders in horizontal format.

Syntax: $ ls -x

Ls –lt command: This command lists the files/folders with the recently accessed file/folder at the top.

Syntax: $ ls -lt

Ls * command: “*” is a meta character. We use * to match 0 or more characters.

Syntax: $ ls *.doc

(Displays all the files that ends with .doc)

Ls ?command: “?” is also a meta character. A question mark (?) Matches with single character.

Syntax: $ ls ???

Displays all the files in the current directory whose names are only three characters long.

14.  MKDIR: Create new folder(s)

The “mkdir” command is used to make a new directory.


$ mkdirmydir_name

This command will create a new directory i.e. “mydir_name”.

$ mkdir-p/tmp/a/b/c

This command will create multiple directories.

I.e. If /tmp/a exists but /tmp/a/b does not, mkdir will create /tmp/a/b before creating /tmp/a/b/c.

15. MV: Move or rename files or directories

The “mv” command is used to move one or more files or directories from one place to another. If both filenames are on the same file system, this results in a simple file rename; otherwise the file content is copied to the new location and the old file is removed.


$ mvmyfilemynewfilename

This command rename “myfile” to “mynewfilename”.

$ mvmyfile ~/myfile

This command will move “myfile” from the current directory to the user’s home directory.

16. PWD: Print working directory

The “pwd” command is used to output the path of the current working directory.


$ pwd

Output: /home/mydir

This command prints out the “/home/mydir”, that means that the directory the user is currently in is /home/mydir.

17. RM: Remove (delete) files

The “rm” command is used to remove/delete files or directories.


$ rm myfile.txt

This command will delete the file “myfile.txt”.

$ rm -i myfile.txt

This command will prompt for confirmation before actually removing it.

18. RMDIR: Remove folder(s)

The “rmdir” command is used to remove the empty directory.


$ rmdirmydir

This command will remove the “mydir” directory

19. KILL: Stop a process from running

The “kill” command can be used to send signals to running processes in order to request the termination of the process


$ kill 111

This command terminates the process with process id ‘111’.

$ kill all

This command kills all the process and the server goes for a restart.

20. TIME: Measure Program Resource Use

The “time” command is used to determine the duration of execution of a particular command.


$ time ls

This command will report how long it took to execute the ls command in terms of user CPU time, system CPU time, and real time.

21. CLEAR: Clear terminal screen

The “clear” command is used to clear the screen.


$ clear

22. EXIT: Exit the Shell

Issuing the “exit” command at the shell prompt will terminate running jobs and cause the shell to exit.

Example: $exit

Common aliases for exit are: “bye”, “logout”, and “lo”.

23. LOGNAME: Print current login name

The “logname” command is used to print the name of the user executing the command.


$ logname

This command returns the name of the currently logged in user.

24. PASSWD: Modify a user password

The “passwd” command is used to change a user’s password.


$ passwd username

This command is used to Change the password for the user named “username”.

25. SU: Substitute user identity

The “su” command can be used to switch user to that of super user or another user.


$ su user2

This command can be used to login as user2 by entering the password. ‘exit’ can be used to exit from it.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>