Pages

Tuesday, September 2, 2014

Other useful UNIX commands


Other useful UNIX commands
quota


All students are allocated a certain amount of disk space on the file system for their personal files, usually about 100Mb. If you go over your quota, you are given 7 days to remove excess files.

To check your current quota and how much of it you have used, type

% quota -v
df

The df command reports on the space left on the file system. For example, to find out how much space is left on the fileserver, type

% df .
du
The du command outputs the number of kilobyes used by each subdirectory. Useful if you have gone over quota and you want to find out which directory has the most files. In your home-directory, type

% du -s *


The -s flag will display only a summary (total size) and the * means all files and directories.
gzip


This reduces the size of a file, thus freeing valuable disk space. For example, type


% ls -l science.txt

and note the size of the file using ls -l . Then to compress science.txt, type


% gzip science.txt

This will compress the file and place it in a file called science.txt.gz


To see the change in size, type ls -l again.


To expand the file, use the gunzip command.

% gunzip science.txt.gz
zcat

zcat will read gzipped files without needing to uncompress them first.

% zcat science.txt.gz

If the text scrolls too fast for you, pipe the output though less .

% zcat science.txt.gz | less
file

file classifies the named files according to the type of data they contain, for example ascii (text), pictures, compressed data, etc.. To report on all files in your home directory, type

% file *

diff


This command compares the contents of two files and displays the differences. Suppose you have a file called file1 and you edit some part of it and save it as file2. To see the differences type

% diff file1 file2

Lines beginning with a < denotes file1, while lines beginning with a > denotes file2.
find

This searches through the directories for files and directories with a given name, date, size, or any other attribute you care to specify. It is a simple command but with many options - you can read the manual by typing man find.

To search for all fies with the extention .txt, starting at the current directory (.) and working through all sub-directories, then printing the name of the file to the screen, type

% find . -name "*.txt" -print


To find files over 1Mb in size, and display the result as a long listing, type

% find . -size +1M -ls
history

The C shell keeps an ordered list of all the commands that you have entered. Each command is given a number according to the order it was entered.

Find utility is used for searching files using the directory information.

1. Write a command to search for the file ‘test’ in the current directory?
find -name test -type f
2. Write a command to search for the file ‘temp’ in ‘/usr’ directory?
find /usr -name temp -type f
3. Write a command to search for zero byte size files in the current directory?
find -size 0 -type f
4. Write a command to list the files that are accessed 5 days ago in the current directory?
find -atime 5 -type f
5. Write a command to list the files that were modified 5 days ago in the current directory?
find -mtime 5 -type f
6. Write a command to search for the files in the current directory which are not owned by any user in the /etc/passwd file?
find . -nouser -type f
7. Write a command to search for the files in ‘/usr’ directory that start with ‘te’?
find /usr -name ‘te*’ -type f
8. Write a command to search for the files that start with ‘te’ in the current directory and then display the contents of the file?
find . -name ‘te*’ -type f -exec cat {} \;
9. Write a command to list the files whose status is changed 5 days ago in the current directory?
find -ctime 5 -type f
10. Write a command to list the files in ‘/usr’ directory that start with ‘ch’ and then display the number of lines in each file?
find /usr -name ‘ch*’ -type f -exec wc -l {} \;

% history (show command history list)


If you are using the C shell, you can use the exclamation character (!) to recall commands easily.


% !! (recall last command)


% !-3 (recall third most recent command)


% !5 (recall 5th command in list)


% !grep (recall last command starting with grep)


You can increase the size of the history buffer by typing


% set history=100

Uniq:

Uniq command in unix or linux system is used to suppress the duplicate lines from a file. It discards all the successive identical lines except one from the input and writes the output.


The syntax of uniq command is
uniq [option] filename

The options of uniq command are:
c : Count of occurrence of each line.
d : Prints only duplicate lines.
D : Print all duplicate lines
f : Avoid comparing first N fields.
i : Ignore case when comparing.
s : Avoid comparing first N characters.
u : Prints only unique lines.
w : Compare no more than N characters in lines
Uniq Command Examples:
First create the following example.txt file in your unix or linux operating system. > cat example.txt Unix operating system unix operating system unix dedicated server linux dedicated server

1. Suppress duplicate lines
The default behavior of the uniq command is to suppress the duplicate line. Note that, you have to pass sorted input to the uniq, as it compares only successive lines. > uniq example.txt unix operating system unix dedicated server linux dedicated server

If the lines in the file are not in sorted order, then use the sort command and then pipe the output to the uniq command. > sort example.txt | uniq

2. Count of lines.
The -c option is used to find how many times each line occurs in the file. It prefixes each line with the count. > uniq -c example.txt 2 unix operating system 1 unix dedicated server 1 linux dedicated server

3. Display only duplicate lines.
You can print only the lines that occur more than once in a file using the -d option. > uniq -d example.txt unix operating system > uniq -D example.txt unix operating system unix operating system

The -D option prints all the duplicate lines.
4. Skip first N fields in comparison.
The -f option is used to skip the first N columns in comparison. Here the fields are delimited by the space character. > uniq -f2 example.txt unix operating system unix dedicated server

In the above example the uniq command, just compares the last fields. For the first two lines, the last field contains the string “system”. Uniq prints the first line and skips the second. Similarly it prints the third line and skips the fourth line.
5. Print only unique lines.
You can skip the duplicate lines and print only unique lines using the -u option > uniq -u example.txt unix dedicated server linux dedicated server

Translate:
Tr stands for translate or transliterate. The tr utility in unix or linux system is used to translate, delete or squeeze characters. The syntax of tr command is


tr [options] set1 [set2]

The options of tr command are:
-c : complements the set of characters in string.
-d : deletes the characters in set1
-s : replaces repeated characters listed in the set1 with single occurrence
-t : truncates set1
Tr command Examples:
1. Convert lower case letters to upper case
The following tr command translates the lower case letters to capital letters in the give string: > echo "linux dedicated server" | tr "[:lower:]" "[:upper:]" LINUX DEDICATED SERVER > echo "linux dedicated server" | tr "[a-z]" "[A-Z]" LINUX DEDICATED SERVER
2. Transform upper case letters to lower case.
Similar to the above example, you can translate the uppercase letters to small letters. > echo "UNIX DEDICATED SERVER" | tr "[:upper:]" "[:lower:]" unix dedicated server > echo "UNIX DEDICATED SERVER" | tr "[A-Z]" "[a-z]" unix dedicated server
3. Replace non-matching characters.
The -c option is used to replace the non-matching characters with another set of characters. > echo "unix" | tr -c "u" "a" uaaa
In the above example, except the character “c” other characters are replaced with “a”
4. Delete non-printable characters
The -d option can be used to delete characters. The following example deletes all the non-printable characters from a file. > tr -cd "[:print:]" < filename
5. Squeezing characters
You can squeeze more than one occurrence of continuous characters with single occurrence. The following example squeezes two or more successive blank spaces into a single space. > echo "linux server" | tr -s " " linux server
Here you can replace the space character with any other character by specifying in set2. > "linux server" | tr -s " " "," linux,server
6. Delete characters
The following example removes the word linux from the string. > echo "linuxserver" | tr -d "linux" server



No comments:

Post a Comment