Вы находитесь на странице: 1из 101

Linux Set Date Use the following syntax to set new data and time: date --set="STRING" For

example, set new data to 2 Oct 2006 18:00:00, type the following command as root user: # date -s "2 OCT 2006 18:00:00" OR # date --set="2 OCT 2006 18:00:00" You can also simplify format using following syntax: # date +%Y%m%d -s "20081128" Linux Set Time To set time use the following syntax: # date +%T -s "10:13:13"

2. DF Df -k A sample output from df command is as follows: $ df Filesystem 1K-blocks Available Use% Mounted on /dev/sda1 132239776 119311504 5% / tmpfs 4021876 4021876 0% /dev/shm /dev/sdb2 30969600 29278696 1% /home/oracle /dev/sdc1 576310180 546964104 1% /home/data Used 6210884 0 117740 71232

So we see that df gives some valuable information on the file systems, their mount points, their memory usage etc. 1. Display Information of all the File Systems If the disk usage of all the file systems is required then use -a option: $ df -a Filesystem 1K-blocks Available Use% Mounted on /dev/sda1 132239776 119311496 5% / proc 0 0 - /proc sysfs 0 0 - /sys devpts 0 0 - /dev/pts tmpfs 4021876 4021876 0% /dev/shm none 0 0 - /proc/sys/fs/binfmt_misc /dev/sdb2 30969600 29278696 1% /home/oracle /dev/sdc1 576310180 546964104 1% /home/data Used 6210892 0 0 0 0 0 117740 71232

So we see that in the output, details of all the file systems and their memory usage is there. 2. Specify the Memory Block Size If you see the output in point 1 above, the second column gives the memory of file system in memory blocks of 1k. df command provides an option through which we can change the size of memory block in the output. Use option -B for this: $ df -B 100

Filesystem 100B-blocks Available Use% Mounted on /dev/sda1 1354135307 1221749720 5% / tmpfs 41184011 41184011 0% /dev/shm /dev/sdb2 317128704 299813848 1% /home/oracle /dev/sdc1 5901416244 5600912425 1% /home/data

Used 63599535 0 1205658 729416

So you see that we specified a block size of 100 and in the output (second column) block size of 100 is displayed. 3. Print Human Readable Sizes We are used to reading the memory in terms of gigabytes, megabytes, etc as its easy to read and remember. df command also provides an option -h to print the memory statistics in human readable format. Option -h stands for human readable format. As shown in the output below, G is used for gigabytes and M is used for megabytes. $ df -h Filesystem Mounted on /dev/sda1 tmpfs /dev/shm /dev/sdb2 /home/oracle /dev/sdc1 /home/data Size 127G 3.9G 30G 550G Used Avail Use% 6.0G 0 115M 70M 114G 3.9G 28G 522G 5% / 0% 1% 1%

4. Display Grand Total in the Output Till now we have seen that only disk usage statistics of individual file systems is produced. If we want to display a grand total of every column then we can use the total flag. Here is an example:

$ df -h --total Filesystem Mounted on /dev/sda1 tmpfs /dev/shm /dev/sdb2 /home/oracle /dev/sdc1 /home/data total

Size 127G 3.9G 30G 550G 710G

Used Avail Use% 6.0G 0 115M 70M 6.2G 114G 3.9G 28G 522G 668G 5% / 0% 1% 1% 1%

So we see that a new row total at the end of the output was produced. 5. List Inodes (Instead of Block Usage) Till now we have seen that df prints the second column as total memory blocks. If information in terms of inode is desired then df provides an option -i for this. $ df -i Filesystem IUse% Mounted on /dev/sda1 1% / tmpfs 1% /dev/shm /dev/sdb2 1% /home/oracle /dev/sdc1 1% /home/data Inodes 8396800 1005469 1966560 36593664 IUsed IFree

65397 8331403 1 1005468 2517 1964043 11 36593653

So we see that information in terms of inodes is displayed. 6. Print File System Type If you wish to print the type of file system in the output, use option T. $ df -T

Filesystem Type 1K-blocks Available Use% Mounted on /dev/sda1 ext4 132239776 119311496 5% / tmpfs tmpfs 4021876 4021876 0% /dev/shm /dev/sdb2 ext2 30969600 29278696 1% /home/oracle /dev/sdc1 ext2 576310180 546964104 1% /home/data

Used 6210892 0 117740 71232

In the above output, we can see all the file systems along with their type is displayed. Apart from using df -T to identify file system, there are 4 other methods to identify the file system types. 7. Include/Exclude Certain File System Type You can also display file systems that belongs to certain type. For example, the following command displays only ext2 file systems. types. $ df -t ext2 Filesystem 1K-blocks Available Use% Mounted on /dev/sdb2 30969600 29278696 1% /home/oracle /dev/sdc1 576310180 546964104 1% /home/data Used 117740 71232

Mount Command The syntax is as follows: mount /dev/XYZN /mount/point WARNING! The mount commands require root user privilege or the corresponding fine-grained privilege,

unless the file system is defined as "user mountable" in the /etc/fstab file. Examples Mount /dev/sda5 (Linux partition) in /data directory: mkdir /data mount /dev/sda5 /data df -H ls /data cd /data Mount /dev/aacd0s1g (FreeBSD UNIX partition) in /salesdata directory: mkdir /salesdata df -H mount /dev/aacd0s1g /salesdata ls /salesdata cd /salesdata Mount /dev/dsk/c1t4d0s0 under Solaris UNIX at /data2, enter: mkdir /data2 mount /dev/dsk/c1t4d0s0 /data2 To remove mount point run: umount /salesdata To mount all file systems listed in /etc/fstab, enter: mount -a Further readings: man mount du

You need use the command line utility called du to displays the file system block usage. In this example find out /tmp dir disk usage statistics (open the terminal and type the following command): $ du /tmp Sample outputs: 4 /tmp/vmware-root 8 /tmp/pulse-xc7xdoM9vB2K 4 /tmp/.X11-unix 4 /tmp/keyring-7qXGnQ 4 /tmp/.exchange-vivek 4 /tmp/.winbindd 8 /tmp/plugtmp 4 /tmp/virtual-vivek.C81Sd0 4 /tmp/VMwareDnD 4 /tmp/ssh-mhNeIv1961 4 /tmp/.ICE-unix 8 /tmp/orbit-vivek 4 /tmp/.esd-1000 31644 /tmp The -h option provides "Human-readable" outpu i.e. you will see it in Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte: $ du -h /tmp Sample outputs: 4.0K 8.0K 4.0K 4.0K 4.0K 4.0K 8.0K 4.0K 4.0K 4.0K 4.0K 8.0K 4.0K /tmp/vmware-root /tmp/pulse-xc7xdoM9vB2K /tmp/.X11-unix /tmp/keyring-7qXGnQ /tmp/.exchange-vivek /tmp/.winbindd /tmp/plugtmp /tmp/virtual-vivek.C81Sd0 /tmp/VMwareDnD /tmp/ssh-mhNeIv1961 /tmp/.ICE-unix /tmp/orbit-vivek /tmp/.esd-1000

33M

/tmp

df: Display Free Disk Space To show statistics about the amount of free disk space on the specified file system or on the file system of which file is a part use the df command as follows: $ df $ df -h Sample outputs: Filesystem Mounted on /dev/wd0a /dev/wd0e 0% /home /dev/wd0d 20% /usr Size 938M 817M 2.9G Used 43.0M 2.0K 573M Avail Capacity 848M 776M 2.2G 5% /

GUI Tools: Disk Usage Analyzer (Gnome Version) Disk Usage Analyzer is a graphical, menu-driven application to analyze disk usage in any UNIX / Linux / BSD Gnome desktop environment. Disk Usage Analyzer can easily scan either the whole filesystem tree, or a specific user-requested directory branch (local or remote). To start this tool visit Gnome menu click on > Applications > Accessories > Select the Disk Usage Analyzer Alternatively, you can start Disk Usage Analyzer from a terminal window, just type: baobab baobab /path/to/dir baobab /home/vivek/mp3/ Sample outputs:

Fig.01: GUI command to check UNIX / Linux / BSD disk space Now, you can:

Start a full filesystem scan Select a specific local directory branch to scan Select a remote server and folder to scan etc

To start a full filesystem scan select Analyzer > Scan Filesystem from the menu, or press on the Scan Filesystem toolbar button. Disk Usage Analyzer will display sizes in the directory tree as allocated space. This means that the displayed sizes refer to the actual disk usage and not to the apparent directory size. If you want to view the apparent file size, uncheck View > Allocated Space .

Fig.02: Directory size

I most commonly use the Linux du command to show Linux directory size, i.e., how large my subdirectories are, including all the files and subdirectories they contain. For example, on the Mac OS X 10.5 system (a Unix system) I'm currently working on, the following du command example displays the size of each subdirectory in my current directory: $ du -hs * 60K 1.2M 4.3M 2.1M 16M 2.5M 1.3M Burn-an-ISO BurnFolders CreateAPodcast-1 CreateAPodcast-2 CreateAPodcast-3 Java-Mac-Jar-Bundler Java-on-Mac

With that command, the -h option of the du command means "human readable" (as in "show me MB and GB"), and the -s means "only show summary information". It's important to note that this command runs in real time, so for very large directories, it can be slow to return your results. Show the size of only specified directories Also, in that du command example, I knew that my current directory just contained the subdirectories shown, so I just issued that du command followed by the "*" wildcard character. If, in a more complicated world, I just wanted to see the size of a few subdirectories in a directory that contained many more subdirectories, I could just specify the directories of interest, like this: $ du -hs Dir1 Dir2 Dir3 Linux du command output without the summary flag As you can see, if I take the -s option off my du command I do indeed get much lengthier output, like this: $ du -h * 60K 376K 188K 296K 1.2M 568K 1.1M 2.4M 628K 28K 4.3M 868K 740K 2.1M 3.8M Burn-an-ISO BurnFolders/BurnFolders BurnFolders/images BurnFolders/images-orig BurnFolders CreateAPodcast-1/CreateAPodcast CreateAPodcast-1/images/Original CreateAPodcast-1/images CreateAPodcast-1/L2HBackups CreateAPodcast-1/OldCSSFiles CreateAPodcast-1 CreateAPodcast-2/CreateAPodcast CreateAPodcast-2/images CreateAPodcast-2 CreateAPodcast-3/CreateAPodcast

1.8M CreateAPodcast-3/images/bg-track 720K CreateAPodcast-3/images/copying 668K CreateAPodcast-3/images/external-track 688K CreateAPodcast-3/images/joining 828K CreateAPodcast-3/images/new-track 1.1M CreateAPodcast-3/images/splitting 6.0M CreateAPodcast-3/images 1.2M CreateAPodcast-3/images-orig/bg-track 468K CreateAPodcast-3/images-orig/copying 444K CreateAPodcast-3/images-orig/externaltrack 464K CreateAPodcast-3/images-orig/joining 556K CreateAPodcast-3/images-orig/new-track 768K CreateAPodcast-3/images-orig/splitting 3.9M CreateAPodcast-3/images-orig 16M CreateAPodcast-3 200K Java-Mac-Jar-Bundler/images 676K Java-Mac-Jar-Bundler/images copy 808K Java-Mac-Jar-Bundler/images-old 328K Java-Mac-Jar-Bundler/java-on-mac 2.5M Java-Mac-Jar-Bundler 308K Java-on-Mac/images 448K Java-on-Mac/java-on-mac 1.3M Java-on-Mac I'll guess that at least 95% of the time I use the -s option, because I'm usually tracking down large directories, or I want to see the directory size before I create an archive.

Chmod command Examples in UNIX and Linux Now let's see some practical and frequently used example of chmod command in UNIX

chmod command Example 1: making read only file in Unix In this example of chmod command in UNIX we will see how to make a file read only by only providing read access to owner. You can also give read access to group and others and keep write access for owner which we will see in subsequent examples. example@localhost~/test ls -lrt stock_trading_systems -rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems* Here file stock_trading_systems has read, write and execute permission -rwxrwxrwx" for all example@localhost~/test chmod 400 stock_trading_systems 400 means 100 000 000 means r-- --- --- i.e. read only for owner example@localhost~/test ls -lrt stock_trading_systems -r-------- 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems Now file is read only and only owner can read it -r--------"

chmod command Example 2: change permissions only for user, group or others. In this example of chmod command we will see how to change file permissions on user, group and others level. You can easily

modify file permission for any of these classes. If you are using text format than u is user , o is other and g is group also r is read , w is write and x is execute. + means adding permission and -is removing permission. example@localhost~/test ls -lrt chmod_examples -r-------- 1 example Domain Users 0 Jul 15 11:42 chmod_examples example@localhost~/test chmod u+w chmod_examples example@localhost~/test ls -lrt chmod_examples -rw------- 1 example Domain Users 0 Jul 15 11:42 chmod_examples Now lets change file permissions only for group by using chmod command example@localhost~/test ls -lrt chmod_examples -rw------- 1 example Domain Users 0 Jul 15 11:42 chmod_examples example@localhost~/test chmod g+w chmod_examples example@localhost~/test ls -lrt chmod_examples -rw--w---- 1 example Domain Users 0 Jul 15 11:42 chmod_examples In this chmod command example we will change permission only for others class without affecting user and group class. example@localhost~/test ls -lrt chmod_examples -rw--w---- 1 example Domain Users 0 Jul 15

11:42 chmod_examples example@localhost~/test chmod o+w chmod_examples example@localhost~/test ls -lrt chmod_examples -rw--w--w- 1 example Domain Users 0 Jul 15 11:42 chmod_examples

Chmod command Example 3: change file permissions for all (user + group + others) In last unix chmod example we learn how to change permission for user, group and others individually but some time its convenient to change permissions for all instead of modifying individual permission for user, group and other. If you are providing permission in text format than a is used for all while u is used for user. example@localhost~/test ls -lrt linux_command.txt -rw--w--w- 1 example Domain Users 0 Jul 15 11:42 linux_command.txt example@localhost~/test chmod a+x linux_command.txt example@localhost~/test ls -lrt linux_command.txt -rwx-wx-wx 1 example Domain Users 0 Jul 15 11:42 linux_command.txt*

Chmod command Example 4: Changing permissions in numeric format of chmod command

Chmod command in UNIX and Linux allows modifying permissions not just on text format which is more readable but also on numeric format where combination of permissions are represented in octal format e.g. 777 where first digit is for user, second is for group and 3rd is for others. Now if you write down 1st digit in binary format it will be written as 111 on which 1st digit is for read permission, 2nd is for write and 3rd is for execute permission. example@localhost~/test ls -lrt unix_command.txt -rw--w--w- 1 example Domain Users 0 Jul 15 11:42 unix_command.txt example@localhost~/test chmod 777 unix_command.txt example@localhost~/test ls -lrt unix_command.txt -rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 unix_command.txt* Chmod command Example 5: How to remove file permission using chmod command Unix In this example of chmod command in UNIX we will see how to remove various permissions from files. You can easily remove read, write or execute permission from file using chmod command in both numeric and text format. Below examples shows removal of execute permission represented by x in text format. example@localhost~/test ls -lrt linux_command.txt -rwx-wx-wx 1 example Domain Users 0 Jul 15 11:42 linux_command.txt*

example@localhost~/test chmod a-x linux_command.txt example@localhost~/test ls -lrt linux_command.txt -rw--w--w- 1 example Domain Users 0 Jul 15 11:42 linux_command.txt Chmod command Example 6: changing permission for directory and subdirectory recursively in Unix This is the most frequently used example of chmod command where we want to provide permission to any directory and all contents inside that directory including files and sub directories. By using R command option of chmod in Unix you can provide permissions recursively to any directory as shown in below example of chmod command. example@localhost~/test ls -lrt total 8.0K -rwxrwxrwx 1 example Domain Users 11:42 unix_command.txt* drwxr-xr-x+ 1 example Domain Users 14:33 stocks/

0 Jul 15 0 Jul 15

example@localhost~/test chmod -R 777 stocks/ example@localhost~/test ls -lrt total 8.0K -rwxrwxrwx 1 example Domain Users 11:42 unix_command.txt* drwxrwxrwx+ 1 example Domain Users 14:33 stocks/

0 Jul 15 0 Jul 15

example@localhost~/test ls -lrt stocks total 0

-rwxrwxrwx 1 example Domain Users 0 Jul 15 14:33 online_stock_exchanges.txt* Chmod command Example 7: How to remove read and write access from file for all So far we have been seeing how to provide read, write and execute permission to file and directory in UNIX and now we will see opposite of that i.e. how to remove read, write and execute access. Its simple in text format because instead of + we are going to use -. Just like + used to add permission will be used to remove permissions. example@localhost~/test ls -lrt stock_trading_systems -rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems* example@localhost~/test chmod a-wx stock_trading_systems example@localhost~/test ls -lrt stock_trading_systems -r--r--r-- 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems Chmod command Example 8: setting execute permission only on directories without touching files Many times we just want to provide directory or subdirectory execute permission without modifying permissions on file just to make those directories searchable. Until I know this command I used to do this by finding all directory and then changing there execute permission but we have a better way to do it by using chmod command in UNIX. You can use "X" (capital X) options to

provide execute permission to only directories without touching files. Lets see an example of chmod command for that: example@localhost~/test ls -lrt total 8.0K -r--r--r-- 1 example Domain Users 11:42 stock_trading_systems drw-rw-rw-+ 1 example Domain Users 14:33 stocks/ example@localhost~/test chmod a+X * example@localhost~/test ls -lrt total 8.0K -r--r--r-- 1 example Domain Users 11:42 stock_trading_systems drwxrwxrwx+ 1 example Domain Users 14:33 stocks/

0 Jul 15 0 Jul 15

0 Jul 15 0 Jul 15

Remember to use X (capital case) if you use x (small case) it will affect all files and directories. Chmod command Example 9: changing mutiple permission for a file or directory in Unix or Linux You can change combination of user + groups or groups+ other in one command to modify permissions of files and directory. Below example of chmod command just doing same its providing execute permission for user and read, execute permission example@localhost~/test ls -lrt total 8.0K -r--r--r-- 1 example Domain Users 11:42 stock_trading_systems drwxrwxrwx+ 1 example Domain Users

0 Jul 15 0 Jul 15

14:33 stocks/ example@localhost~/test chmod u+x,g+x stock_trading_systems example@localhost~/test ls -lrt stock_trading_systems -r-xr-xr-- 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems* Chmod command Example 10: How to copy permission from one file to another in Unix This is very interesting example of chmod command in UNIX which copies permission from one file to another. You can easily reference source file and copy all permissions on that file to destination file as shown in following chmod example: example@localhost~/test ls -lrt future_trading -rwxrwxrwx 1 example Domain Users 0 Jul 15 15:30 future_trading* example@localhost~/test ls -lrt stock_trading_systems -r--r--r-- 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems example@localhost~/test chmod --reference=stock_trading_systems future_trading example@localhost~/test ls -lrt future_trading -r--r--r-- 1 example Domain Users 0 Jul 15 15:30 future_trading

These were some of frequently used example of chmod command in UNIX or Linux. Chmod command is as useful as UNIX find command or grep command and knowing how to change file permissions is essential skill while working in UNIX. Please share if you have any other example of chmod command which we should be aware of. Read more: http://javarevisited.blogspot.com/2012/03/10-example-ofchmod-command-in-unix.html#ixzz1ybsKBzU0 sort [OPTION]... [FILE]... DESCRIPTION Write sorted concatenation of all FILE(s) to standard output. Ordering options: Mandatory arguments to long options are mandatory for short options too. -b, --ignore-leading-blanks ignore leading blanks -d, --dictionary-order consider only blanks and alphanumeric characters -f, --ignore-case fold lower case to upper case characters -g, --general-numeric-sort compare according to general numerical value -i, --ignore-nonprinting consider only printable characters -M, --month-sort compare (unknown) < `JAN' < ... < `DEC' -n, --numeric-sort compare according to string numerical value -r, --reverse reverse the result of comparisons Other options:

-c, --check check whether input is sorted; do not sort -k, --key=POS1[,POS2] start a key at POS1, end it at POS 2 (origin 1) -m, --merge merge already sorted files; do not sort -o, --output=FILE write result to FILE instead of standard output -s, --stable stabilize sort by disabling last-resort comparison -S, --buffer-size=SIZE use SIZE for main memory buffer -t, --field-separator=SEP use SEP instead of non- to whitespace transition -T, --temporary-directory=DIR use DIR for temporaries, not $TMPDIR or /tmp multiple options specify multiple directories -u, --unique with -c: check for strict ordering otherwise: output only the first of an equal run -z, --zero-terminated end lines with 0 byte, not newline --help display this help and exit --version output version information and exit In order to sort the lines in a file you would use a command line like: $ sort -k2 test.txt which sorts the file "test.txt" according to the characters starting at the second column (k2 refers to the second column). Assuming the input file contents is: 1, Justin Timberlake, Title 545, Price $7.30

2, Taylor Swift, Title 723, Price $7.90 3, Mick Jagger, Title 610, Price $7.90 4, Lady Gaga, Title 118, Price $7.30 5, Johnny Cash, Title 482, Price $6.50 6, Elvis Presley, Title 335, Price $7.30 7, John Lennon, Title 271, Price $7.90 8, Michael Jackson, Title 373, Price $5.50 The sorted output would look like this: 6, Elvis Presley, Title 335, Price $6.30 7, John Lennon, Title 271, Price $7.90 5, Johnny Cash, Title 482, Price $6.50 1, Justin Timberlake, Title 545, Price $6.30 4, Lady Gaga, Title 118, Price $6.30 8, Michael Jackson, Title 373, Price $5.50 3, Mick Jagger, Title 610, Price $7.90 2, Taylor Swift, Title 723, Price $7.90 If you sorted the file with -k4 (using the line contents starting at column 4), the output would be: $ sort -k4 test.txt 4, Lady Gaga, Title 118, Price $6.30 7, John Lennon, Title 271, Price $7.90 6, Elvis Presley, Title 335, Price $6.30 8, Michael Jackson, Title 373, Price $5.50 5, Johnny Cash, Title 482, Price $6.50 1, Justin Timberlake, Title 545, Price $6.30 3, Mick Jagger, Title 610, Price $7.90 2, Taylor Swift, Title 723, Price $7.90 And $ sort -k7 test.txt Would produce 8, Michael Jackson, Title 373, Price $5.50 1, Justin Timberlake, Title 545, Price $6.30

4, Lady Gaga, Title 118, Price $6.30 6, Elvis Presley, Title 335, Price $6.30 5, Johnny Cash, Title 482, Price $6.50 2, Taylor Swift, Title 723, Price $7.90 3, Mick Jagger, Title 610, Price $7.90 7, John Lennon, Title 271, Price $7.90 The -r option will reverse the sorting. For example, $ sort -k7 -r test.txt Yields 7, John Lennon, Title 271, Price $7.90 3, Mick Jagger, Title 610, Price $7.90 2, Taylor Swift, Title 723, Price $7.90 5, Johnny Cash, Title 482, Price $6.50 6, Elvis Presley, Title 335, Price $6.30 4, Lady Gaga, Title 118, Price $6.30 1, Justin Timberlake, Title 545, Price $6.30 8, Michael Jackson, Title 373, Price $5.50 In order to save the sorted list in a file you could use the "redirect" operator: sort -k7 -r test.txt > test_new.txt where "test_new.txt" would be the new file. Of course you can also apply the sort command to the output of a stream, such as the "pipe" operator: $ ls -al | sort -r -n -k5 This sorts the output of the file listing generated by ls by file size, starting with largest files. The -n operator specifies "numeric" sorting as compared to alphabetic.

With + option it's possible to give a sorting key man sort Code: `+POS1[-POS2]' The obsolete, traditional option for specifying a sort field. The field consists of the line between POS1 and up to but _not including_ POS2 (or the end of the line if POS2 is omitted). Fields and character positions are numbered starting with 0. [...] To sort the file below on the third field (area code): Jim Alchin 212121 Seattle Bill Gates 404404 Seattle Steve Jobs 246810 Nevada Scott Neally 212277 Los Angeles $ sort -k 3,3 people.txt> sorted.txt or using the 'old' syntax: $ sort +2 -3 people.txt> sorted2.txt Have you tried the example?

Here more examples... ------------------------------------------------

$ cat example.txt Jon 200 Bill 30 Carl 100 Bernard 5 $ sort -k 2 -n example.txt Bernard 5 Bill 30 Carl 100 Jon 200 -----------------------------------------------$ ls -l | sort -n -r -k 5 -rw-r--r-- 1 root root 1057 1 mar 20:12 ring+star -rw-r--r-- 1 root root 727 1 mar 20:12 A_star -rw-r--r-- 1 root root 535 1 mar 20:12 star2 -rw-r--r-- 1 root root 505 1 mar 20:12 spirals_and_stars -rw-r--r-- 1 root root 503 1 mar 20:12 curves -rw-r--r-- 1 root root 475 1 mar 20:12 simily -rw-r--r-- 1 root root 458 1 mar 20:12 ring -rw-r--r-- 1 root root 415 1 mar 20:12 stars -rw-r--r-- 1 root root 387 1 mar 20:12 sprial -rw-r--r-- 1 root root 377 1 mar 20:12 polys

Task: Append Data To A Text File To append (add data to existing) data to a file called foo.txt, enter: cat >>foo.txt

Task: Number All Output Lines Type the following command: cat -n filename cat --number filename Task: View Non-printing Characters To display TAB characters as ^I, enter: cat -T filename To display $ at end of each line, enter: cat -E filename cat --show-ends filename Use ^ and M- notation, except for LFD and TAB and show all nonprinting: cat -v filename cat --show-nonprinting filename To show all, enter: cat -A fileName OR

cat -vET fileName Sample outputs:

Fig.02: Unix / Linux cat command: View Non-printing Characters Viewing All Files You can simply use the shell wildcard as follows: cat * To view only (c files) *.c files, enter:

cat *.c Syntax: The syntax is as follows: cat cat cat cat file1 > file2 file3 | command file4 | grep something

Examples: cat file1 cat file1 file2 > all cat file1 >> file2

What is wc ? wc prints the number of lines, words and bytes in the given input. wc is the abbreviation of word count. 3 wc Examples Print the number of lines, words and characters in a file To view the number of lines, words and characters in a file, give the filename as argument to the wc command as shown below. Syntax: $ wc filename X Y Z filename

X Number of lines Y Number of words Z Number of bytes filename name of the file

$ wc demofile.txt 36 104 570 demofile.txt The above output indicates that the demofile.txt has 36 lines, 104 words, and 570 bytes. Print specific counts as required You can use -l option to print only the line count as shown below. $ wc -l demofile.txt 36 demofile.txt The same way you can use the following options for specific requirements such as,

-w : print only word count -c : print only the byte count

You can also use any combination of above options. Print the length of longest line You can use -L option to print the length of longest line in the file. $ wc -L demofile.txt 84 demofile.txt Syntax and Options Short Option -c -m -l Long Option bytes chars lines Option Description print the byte counts print the character counts print the newline counts

-L -w

files0from=F max-linelength words

read input from the files specified by NULterminated names in file F print the length of the longest line print the word counts

What is cmp? cmp command is used to compare two files byte by byte. 5 cmp Examples Two provide examples for this command, lets consider two files : file1.txt $ cat file1.txt Hi My name is Himanshu file2.txt $ cat file2.txt Hi My name is Himanshu Arora 1. Directly compare using cmp If we straight away try to compare the two files mentioned above, then we see : $ cmp file1.txt file2.txt file1.txt file2.txt differ: byte 23, line 1 So we see that cmp utility instantly recognized the difference between contents in two files. 2. Skip same number of initial bytes from both input files

If we want to skip some initial bytes of input files and then start the comparison then -i option can be used $ cmp -i 5 file1.txt file2.txt file1.txt file2.txt differ: byte 18, line 1 So we see that the initial 5 bytes were skipped and hence cmp complains that byte 18 differs (which in example 1 was byte 23) 3. Skip different number of initial bytes from both input files Another case could be when we would want to skip x bytes of input in file1 and y bytes of input in file2. This could be achieved as follows : $ cmp -i 5:2 file1.txt file2.txt file1.txt file2.txt differ: byte 2, line 1 So we see that we can give the option -i with x:y kind of input for this case. The output shows that this time the difference occurred very early at the second byte. 4. Display bytes that differ If in the example 3 above we would also like to know the values of individual differing bytes being compared then the -l option can be used $ cmp -l -i 5:2 file1.txt file2.txt 2 156 115 3 141 171 4 155 40 5 145 156 6 40 141 7 151 155 8 163 145 10 110 151 11 151 163 12 155 40 13 141 110

14 156 151 15 163 155 16 150 141 17 165 156 18 12 163 cmp: EOF on file1.txt So we see can easily observe the difference in byte2 above. 5. Upper limit on number of bytes to compare Also, we can put a limit on number of bytes to compare. This can be used when we want to compare only a specified number of bytes and not the complete file. This can be achieved through -n option which is followed by the number of bytes. So in the above example 4 if we want to put an upper limit on the bytes to be compared, it can be done as follows : $ cmp -l -n 10 -i 5:2 file1.txt file2.txt 2 156 115 3 141 171 4 155 40 5 145 156 6 40 141 7 151 155 8 163 145 10 110 151 Syntax and Options cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]] Short Option Long Option Option Description -b print-bytes Print differing bytes. Skip the first SKIP bytes of -i SKIP ignore-initial=SKIP input Skip the first SKIP1 bytes of -i ignoreFILE1 and the first SKIP2 SKIP1:SKIP2 initial=SKIP1:SKIP2 bytes of FILE2. no-preserve-root do not treat `/ specially (the

-l -n LIMIT -s -v Comm.

verbose bytes=LIMIT quiet silent version help

default) Output byte numbers and values of all differing bytes. Compare at most LIMIT bytes. Output nothing; yield exit status only. Output version info. Output this help.

NAME comm - select or reject lines common to two files SYNOPSIS comm [-123] file1 file2 OPTIONS The following options are supported: -1 Suppresses the output column of lines unique to file1. -2 Suppresses the output column of lines unique to file2. -3 Suppresses the output column of lines duplicated in file1 and file2.

NAME diff - find differences between two files SYNOPSIS diff [options] from-file to-file

DESCRIPTION In the simplest case, diff compares the contents of the two files fromfile and to-file. A file name of - stands for text read from the standard input. As a special case, diff - - compares a copy of standard input to itself. If from-file is a directory and to-file is not, diff compares the file in from-file whose file name is that of to-file, and vice versa. The nondirectory file must not be -. If both from-file and to-file are directories, diff compares corresponding files in both directories, in alphabetical order; this comparison is not recursive unless the -r or --recursive option is given. diff never compares the actual contents of a directory as if it were a file. The file that is fully specified may not be standard input, because standard input is nameless and the notion of ``file with the same name'' does not apply. diff options begin with -, so normally from-file and to-file may not begin with -. However, -- as an argument by itself treats the remaining arguments as file names even if they begin with -. Options Below is a summary of all of the options that GNU diff accepts. Most options have two equivalent names, one of which is a single letter preceded by -, and the other of which is a long name preceded by --. Multiple single letter options (unless they take an argument) can be combined into a single command line word: -ac is equivalent to -a -c. Long named options can be abbreviated to any unique prefix of their name. Brackets ([ and ]) indicate that an option takes an optional argument. -lines Show lines (an integer) lines of context. This option does not specify an output format by itself; it has no effect unless it is combined with -c or -u. This option is obsolete. For proper operation, patch typically needs at least two lines of context. -a

Treat all files as text and compare them line-by-line, even if they do not seem to be text. -b Ignore changes in amount of white space. -B Ignore changes that just insert or delete blank lines. --brief Report only whether the files differ, not the details of the differences. -c Use the context output format. -C lines --context[=lines] Use the context output format, showing lines (an integer) lines of context, or three if lines is not given. For proper operation, patch typically needs at least two lines of context. --changed-group-format=format Use format to output a line group containing differing lines from both files in if-then-else format. -d Change the algorithm to perhaps find a smaller set of changes. This makes diff slower (sometimes much slower). -D name Make merged if-then-else format output, conditional on the preprocessor macro name. -e --ed Make output that is a valid ed script. --exclude=pattern When comparing directories, ignore files and subdirectories whose basenames match pattern. --exclude-from=file When comparing directories, ignore files and subdirectories whose basenames match any pattern contained in file. --expand-tabs

Expand tabs to spaces in the output, to preserve the alignment of tabs in the input files. -f Make output that looks vaguely like an ed script but has changes in the order they appear in the file. -F regexp In context and unified format, for each hunk of differences, show some of the last preceding line that matches regexp. --forward-ed Make output that looks vaguely like an ed script but has changes in the order they appear in the file. -h This option currently has no effect; it is present for Unix compatibility. -H Use heuristics to speed handling of large files that have numerous scattered small changes. --horizon-lines=lines Do not discard the last lines lines of the common prefix and the first lines lines of the common suffix. -i Ignore changes in case; consider upper- and lower-case letters equivalent. -I regexp Ignore changes that just insert or delete lines that match regexp. --ifdef=name Make merged if-then-else format output, conditional on the preprocessor macro name. --ignore-all-space Ignore white space when comparing lines. --ignore-blank-lines Ignore changes that just insert or delete blank lines. --ignore-case Ignore changes in case; consider upper- and lower-case to be the same. --ignore-matching-lines=regexp Ignore changes that just insert or delete lines that match regexp.

--ignore-space-change Ignore changes in amount of white space. --initial-tab Output a tab rather than a space before the text of a line in normal or context format. This causes the alignment of tabs in the line to look normal. -l Pass the output through pr to paginate it. -L label --label=label Use label instead of the file name in the context format and unified format headers. --left-column Print only the left column of two common lines in side by side format. --line-format=format Use format to output all input lines in in-then-else format. --minimal Change the algorithm to perhaps find a smaller set of changes. This makes diff slower (sometimes much slower). -n Output RCS-format diffs; like -f except that each command specifies the number of lines affected. -N --new-file In directory comparison, if a file is found in only one directory, treat it as present but empty in the other directory. --new-group-format=format Use format to output a group of lines taken from just the second file in if-then-else format. --new-line-format=format Use format to output a line taken from just the second file in ifthen-else format. --old-group-format=format

Use format to output a group of lines taken from just the first file in if-then-else format. --old-line-format=format Use format to output a line taken from just the first file in if-thenelse format. -p Show which C function each change is in. -P When comparing directories, if a file appears only in the second directory of the two, treat it as present but empty in the other. --paginate Pass the output through pr to paginate it. -q Report only whether the files differ, not the details of the differences. -r When comparing directories, recursively compare any subdirectories found. --rcs Output RCS-format diffs; like -f except that each command specifies the number of lines affected. --recursive When comparing directories, recursively compare any subdirectories found. --report-identical-files -s Report when two files are the same. -S file When comparing directories, start with the file file. This is used for resuming an aborted comparison. --from-file=file Compare file to all operands. file can be a directory. --to-file=file Compare all operands to file. file can be a directory. --sdiff-merge-assist

Print extra information to help sdiff. sdiff uses this option when it runs diff. This option is not intended for users to use directly. --show-c-function Show which C function each change is in. --show-function-line=regexp In context and unified format, for each hunk of differences, show some of the last preceding line that matches regexp. --side-by-side Use the side by side output format. --speed-large-files Use heuristics to speed handling of large files that have numerous scattered small changes. --starting-file=file When comparing directories, start with the file file. This is used for resuming an aborted comparison. --suppress-common-lines Do not print common lines in side by side format. -t Expand tabs to spaces in the output, to preserve the alignment of tabs in the input files. -T Output a tab rather than a space before the text of a line in normal or context format. This causes the alignment of tabs in the line to look normal. --text Treat all files as text and compare them line-by-line, even if they do not appear to be text. -u Use the unified output format. --unchanged-group-format=format Use format to output a group of common lines taken from both files in if-then-else format. --unchanged-line-format=format Use format to output a line common to both files in if-then-else format. --unidirectional-new-file

When comparing directories, if a file appears only in the second directory of the two, treat it as present but empty in the other. -U lines --unified[=lines] Use the unified output format, showing lines (an integer) lines of context, or three if lines is not given. For proper operation, patch typically needs at least two lines of context. -v --version Output the version number of diff. -w Ignore white space when comparing lines. -W columns --width=columns Use an output width of columns in side by side format. -x pattern When comparing directories, ignore files and subdirectories whose basenames match pattern. -X file When comparing directories, ignore files and subdirectories whose basenames match any pattern contained in file. -y Use the side by side output format.

Environmental Commands logout or exit passwd date whoami or who am i who end terminal session change password print date & time list current user in that window list all users logged onto the workstation

man command1

print on-line UNIX manual page for command1

start file transfer protocol to transfer file between local ftp hostname and remote workstations. Remote workstation is named hostname telnet log onto remote workstation named hostname hostname df -k print list of disks and current capacity (in kilobytes) setenv list environmental variables which list location of executable command command File Manipulation list contents of current directory. Examples: ls file.1 lists presence of file.1 ls -l lists presence of file.1 with expanded information ls -a lists contents of current directory, including files that start with "." opens jot editor; reads file.1 into jot for editing show contents of file.1 on screen show contents of file.1 on screen, but doesn't scroll past top of screen show first 20 lines of file.1 on screen show last 20 lines of file.1 on screen shows differences between file.1 and file.2 counts lines, words, and characters in file file.1. Variations: wc -l counts lines in file file.1 wc -w counts words in file file.1 wc -m counts characters in file file.1 copies file.1 to file.2. If file.2 exists, it is overwritten. Original file.1 is not removed moves file.1 to file.2. If file.2 exists, it is overwritten. Original file.1 is removed remove file.1. No utilities exist to retrieve it

ls

jot file.1 cat file.1 more file.1 head file.1 tail file.1 diff file.1 file.2 wc

cp file.1 file.2 mv file.1 file.2 rm file.1

grep PATTERN print all lines that contain PATTERN in file.1 file.1 lpr file.1 print file.1 on default laser printer Show list of available printers and questions to direct lpr your printing of a file to a particular printer print size (in kilobytes) of current directory ("."). Variations: du -sk du -sk * prints sizes (in kilobytes) of all files and sub-directories du -sk file.1 prints size (in kilobytes) of file.1 reduce the size of file.1 and replace it with a file compress file.1 named file.1.Z. This compressed file is NOT a (readable) ASCII file. uncompress restore the compressed file file.1.Z file.1.Z find the loation of file.1 in the directory and subdirectories. Variations: find . -name find . -name "*pattern" -print finds all files that file.1 -print end with pattern. find . -name file.1 -exec rm '{}' ';' removes all files named file.1. grep pattern print only the lines of file.1 that contain pattern file.1 displays contents of file.1 sorted alphabetically. Variations: sort -n file.1 displays contents of file.1 sorted sort file.1 numerically sort -r file.1 displays contents of file.1 sorted alphabetically in reverse order. Directory Manipulation pwd cd dir.1 mkdir dir.1 show the directory that you are in (present working directory) change directory to dir.1 make new directory dir.1

rmdir dir.1 rm -r dir.1 cp -r dir.1 dir.2 mv file.1 dir.1 ls du -sk dir.1 tar -cvf dir.1.tar dir.1 Process Control command1& ps -ef kill pid1 <control-c> <control-z> jobs kill %1 top osview

remove EMPTY directory dir.1 remove directory dir.2 AND its contents copy dir.1 (and its contents) to dir.2 move file.1 to dir.1 show contents of current directory. Variations: ls dir.1 shows contents of dir.1 ls -d dir.1 shows PRESENCE of dir.1 show sum of size (in kilobytes) of dir.1 and its contents store an image of dir.1 and it's contents in file file.1

execute command1 in background print expanded list of all processes remove process pid1 interrupt current process suspend current process display background and suspended processes remove suspended process #1 display the current, most computer-intensive commands display the operating system statitistics

System Security The following mechanism is the ONLY guaranteed method of protecting your work. Each file and directory is owned by a user, and each user belongs to a group. By default, users own their home directory (the current directory when the user logs in) and the contents of the home directory. Most other files and directories are owned by "root" and other special users.

The user assigns a type of privilege to each file and directory owned by the user. By default, the privilege is rwxr-xr-x. The first three characters of rwxr-xr-x indicate that the owner can read, write, and execute the file (or directory). The middle three characters indicate that all other users in the same group as the owner can read and execute the file (or directory), but cannot write onto the file (or directory), as indicated by the middle "-" character. The last three characters indicate that everyone else on the system can read and execute the file (or directory), but cannot write onto the file (or directory), as indicated by the last "-" character. To change the owner's permissions of a file (or directory), the owner can type: chown u+r file.1 chown u+w file.1 chown u+x file.1 chown u-r file.1 chown u-w file.1 chown u-x file.1 To change the group's permissions of a file (or directory), the owner can type: chown g+r file.1 chown g+w file.1 chown g+x file.1 chown g-r file.1 chown g-w file.1 chown g-x file.1 To change the permissions of a file (or directory) for everyone else, the owner can type: chown o+r file.1 chown o+w file.1 chown o+x file.1 chown o-r file.1 chown o-w file.1 chown o-x file.1

SYNOPSIS

touch [-acm] [-f agefile] [-r agefile] [[-t] time] file ... touch [-acm] time file ... DESCRIPTION The touch command changes certain dates for each file argument. By default, touch sets both the date of last file modification and the date of last file access to the current time. This is useful for maintaining correct release times for software and is particularly useful in conjunction with the MKS Make software development facility. Options -a sets only the access time. -c does not create any files that do not already exist. Normally, touch creates such files. -m sets only the modification time. If you do not specify -a or -m, touch behaves as though you specified both. To tell touch to use a time other than the current, use one of the following options: -f agefile is an obsolete version of the POSIX-compliant -r option. -r agefile

sets the access and modification times (as indicated by the other options) to those kept for agefile. -t time specifies a particular time using this format: [[[[cc]yy]MM]dd]hhmm[.ss] where cc is the optional first 2 digits of the year, yy is the optional last 2 digits of the year, MM is the optional number of the month (01-12), dd is the optional day of the month, hh is the hour in 24 hour format (required), mm is the minutes (required), ss is the optional seconds. An obsolete (but still supported) version of this command lets you omit the -t, but the format is: MMddhhmmyy[yy] EXAMPLES touch newfile sets the modification time of newfile to the present. touch -t 8001031305 oldfile sets the modification time of oldfile to 13:05 on January 3, 1980. touch -r oldfile newfile sets the modification time of newfile to that of oldfile.

The unix touch command Posted on September 11, 2008

This post is a quick ref on the linux touch command. All the examples have been tested on Linux. This command is used to update the access and modification times of files.

touchs syntax

touch [option] file_name(s) touch file1 file2 file3

Here some examples:

# touch /tmp/file # ls -l /tmp/file rw-r--r-- 1 charlybr charlybr 0 Sep 10 16:13 /tmp/file

Update access and modification time to current time:

# ls -l /tmp/file rw-r--r-- 1 charlybr charlybr 0 Sep 10 16:13 /tmp/file # touch /tmp/file # ls -l /tmp/file rw-r--r-- 1 charlybr charlybr 0 Sep 10 16:14 /tmp/file

Update access and modification time to a specified timestamp ([[CC]YY]MMDDhhmm[.ss] format):

# touch -t 09091842 /tmp/file # ls -l /tmp/file -rw-r--r-- 1 charlybr charlybr 0 Sep 18:42 /tmp/file

Update access and modification time to a specified date

# touch -d '9 Sep' /tmp/file # ls -l /tmp/file -rw-r--r-- 1 charlybr charlybr 0 Sep 9 00:00 /tmp/file # touch -d '9 Sep 2008 13:14' /tmp/file # ls -l /tmp/file -rw-r--r-- 1 charlybr charlybr 0 Sep 9 13:14 /tmp/file Hack 25. Cut Command Examples by Ramesh Cut command can be used to display only specific columns from a text file or other command outputs. Following are some of the examples. Display the 1st field (employee name) from a colon delimited file $ cut -d: -f 1 names.txt Emma Thomas Alex Jason Madison Randy Sanjay Gupta Nisha Singh Display 1st and 3rd field from a colon delimited file $ cut -d: -f 1,3 names.txt Emma Thomas:Marketing Alex Jason:Sales Madison Randy:Product Development Sanjay Gupta:Support Nisha Singh:Sales

Display only the first 8 characters of every line in a file $ cut -c 1-8 names.txt Emma Tho Alex Jas Madison Sanjay G Nisha Si Misc Cut command examples Displays the unix login names for all the users in the system. $ cut -d: -f1 /etc/passwd Displays the total memory available on the system. $ free | tr -s ' ' | sed '/^Mem/!d' | cut -d" " -f2 Unix Cut Command Example We will see the usage of cut command by considering the below text file as an example > cat file.txt unix or linux os is unix good os is linux good os 1. Write a unix/linux cut command to print characters by position? The cut command can be used to print characters in a line by specifying the position of the characters. To print the characters in a line, use the -c option in cut command cut -c4 file.txt x

u l The above cut command prints the fourth character in each line of the file. You can print more than one character at a time by specifying the character positions in a comma separated list as shown in the below example cut -c4,6 file.txt xo ui ln This command prints the fourth and sixth character in each line. 2.Write a unix/linux cut command to print characters by range? You can print a range of characters in a line by specifying the start and end position of the characters. cut -c4-7 file.txt x or unix linu The above cut command prints the characters from fourth position to the seventh position in each line. To print the first six characters in a line, omit the start position and specify only the end position. cut -c-6 file.txt unix o is uni is lin To print the characters from tenth position to the end, specify only the start position and omit the end position. cut -c10- file.txt inux os

ood os good os If you omit the start and end positions, then the cut command prints the entire line. cut -c- file.txt 3.Write a unix/linux cut command to print the fields using the delimiter? You can use the cut command just as awk command to extract the fields in a file using a delimiter. The -d option in cut command can be used to specify the delimiter and -f option is used to specify the field position. cut -d' ' -f2 file.txt or unix linux This command prints the second field in each line by treating the space as delimiter. You can print more than one field by specifying the position of the fields in a comma delimited list. cut -d' ' -f2,3 file.txt or linux unix good linux good The above command prints the second and third field in each line. Note: If the delimiter you specified is not exists in the line, then the cut command prints the entire line. To suppress these lines use the -s option in cut command. 4. Write a unix/linux cut command to display range of fields? You can print a range of fields by specifying the start and end position.

cut -d' ' -f1-3 file.txt The above command prints the first, second and third fields. To print the first three fields, you can ignore the start position and specify only the end position. cut -d' ' -f-3 file.txt To print the fields from second fields to last field, you can omit the last field position. cut -d' ' -f2- file.txt 5. Write a unix/linux cut command to display the first field from /etc/passwd file? The /etc/passwd is a delimited file and the delimiter is a colon (:). The cut command to display the first field in /etc/passwd file is cut -d':' -f1 /etc/passwd 6. The input file contains the below text > cat filenames.txt logfile.dat sum.pl add_int.sh Using the cut command extract the portion after the dot. First reverse the text in each line and then apply the command on it. rev filenames.txt | cut -d'.' -f1

Unix Shell commands - 9:07 AM - 11 comments

Unix cut command with real examples Uinx Cut command displays selected sections from each line of a file. Unix Cut command Syntax : cut [OPTION]... [FILE]... Linux Cut commnd options with examples Unix cut command treats input data is made up of one range, or many ranges separated by delimiter. N character or field, counted from 1 N- from Nth byte, character or field, to end of line N-M from Nth to Mth (included) byte, character or field -M from first to Mth (included) byte, character or field

Let see the options of unix commands with examples. My sample file contents $ cat passwdfile unixuser:x:501:506:Unix User:/home/unixuser:/bin/bash oradba:x:502:506:DBA User:/home/oradba:/bin/bash

scripter:x:1658:506:Scripter World:/home/scripter:/bin/bash $ Unix cut c option -c option specifies to output to display only these character positions. To display output of 10 character(N character) of each line $ cut -c 10 passwdfile x 5 x $ To display output of 5 and 10 characters(N,M characters) of each line $ cut -c 5,10 passwdfile ux b5 px $ To display output from 5th to 10th characters(N-M characters) of each line $ cut -c 5-10 passwdfile user:x

ba:x:5 pter:x $ To display output upto 10th character(-M charcters) of each line $ cut -c -10 passwdfile unixuser:x oradba:x:5 scripter:x $ To display output from 10th character(N- charcters) to end of the each line $ cut -c 10- passwdfile x:501:506:Unix User:/home/unixuser:/bin/bash 502:506:DBA User:/home/oradba:/bin/bash x:1658:506:Scripter World:/home/scripter:/bin/bash $ Unix cut -d option and Unix cut -f option -d The character following -d is the field delimiter . Default is tab. Space or other characters with special meaning to the shell must be quoted. delim can be a multi-byte character. -f specifies the fields to display from the list of fields formed from the input data separate by delimter(-d) .Fields are counted from one and separate multiple values by comma (,) To get the user description(5th field) from our example file

passwdfile use unix cut -d -f options at once Use : as delimiter and display 5th field $ cut -d ":" -f5 passwdfile Unix User DBA User Scripter World $ To display user description(5th filed) and user home directory(6th field) use comma with ":" as delimiter $ cut -d ":" -f5,6 passwdfile Unix User:/home/unixuser DBA User:/home/oradba Scripter World:/home/scripter $ To display fields up to user description in our passwd file ,use before the user description(5th filed) field $ cut -d ":" -f-5 passwdfile unixuser:x:501:506:Unix User oradba:x:502:506:DBA User scripter:x:1658:506:Scripter World $ To display fields from user description ,Use - after the user description(5th filed) field $ cut -d ":" -f5- passwdfile Unix User:/home/unixuser:/bin/bash DBA User:/home/oradba:/bin/bash

Scripter World:/home/scripter:/bin/bash $ Unix cut last character To display a last character of a string STRING="unixuser:x:501:506:Unix User:/home/unixuser:/bin/bash" echo ${STRING} awk '$0=$NF' FS= $ STRING="unixuser:x:501:506:Unix User:/home/unixuser:/bin/bash" $ echo ${STRING} awk '$0=$NF' FS= h $

Use cut last field or column To display a last field of a string with a delimter (Here delimter is ":" colon) STRING="unixuser:x:501:506:Unix User:/home/unixuser:/bin/bash" echo ${STRING} awk '$0=$NF' FS=":" $ STRING="unixuser:x:501:506:Unix User:/home/unixuser:/bin/bash" $ echo ${STRING} awk '$0=$NF' FS=":" /bin/bash $

First create the following demo_file that will be used in the examples below to demonstrate grep command. $ cat demo_file THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE. this line is the 1st lower case line in this file. This Line Has All Its First Character Of The Word With Upper Case. Two lines above this line is empty. And this is the last line. 1. Search for the given string in a single file The basic usage of grep command is to search for a specific string in the specified file as shown below. Syntax: grep "literal_string" filename $ grep "this" demo_file this line is the 1st lower case line in this file. Two lines above this line is empty. 2. Checking for the given string in multiple files. Syntax: grep "string" FILE_PATTERN This is also a basic usage of grep command. For this example, let us copy the demo_file to demo_file1. The grep output will also include the file name in front of the line that matched the specific pattern as shown below. When the Linux shell sees the meta character, it does the expansion and gives all the files as input to grep. $ cp demo_file demo_file1

$ grep "this" demo_* demo_file:this line is the 1st lower case line in this file. demo_file:Two lines above this line is empty. demo_file:And this is the last line. demo_file1:this line is the 1st lower case line in this file. demo_file1:Two lines above this line is empty. demo_file1:And this is the last line. 3. Case insensitive search using grep -i Syntax: grep -i "string" FILE This is also a basic usage of the grep. This searches for the given string/pattern case insensitively. So it matches all the words such as the, THE and The case insensitively as shown below. $ grep -i "the" demo_file THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE. this line is the 1st lower case line in this file. This Line Has All Its First Character Of The Word With Upper Case. And this is the last line. 4. Match regular expression in files Syntax: grep "REGEX" filename This is a very powerful feature, if you can use use regular expression effectively. In the following example, it searches for all the pattern that starts with lines and ends with empty with anything in-between. i.e To search lines[anything in-between]empty in the demo_file.

$ grep "lines.*empty" demo_file Two lines above this line is empty. From documentation of grep: A regular expression may be followed by one of several repetition operators:

? The preceding item is optional and matched at most once. * The preceding item will be matched zero or more times. + The preceding item will be matched one or more times. {n} The preceding item is matched exactly n times. {n,} The preceding item is matched n or more times. {,m} The preceding item is matched at most m times. {n,m} The preceding item is matched at least n times, but not more than m times.

5. Checking for full words, not for sub-strings using grep -w If you want to search for a word, and to avoid it to match the substrings use -w option. Just doing out a normal search will show out all the lines. The following example is the regular grep where it is searching for is. When you search for is, without any option it will show out is, his, this and everything which has the substring is. $ grep -i "is" demo_file THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE. this line is the 1st lower case line in this file. This Line Has All Its First Character Of The Word With Upper Case. Two lines above this line is empty. And this is the last line.

The following example is the WORD grep where it is searching only for the word is. Please note that this output does not contain the line This Line Has All Its First Character Of The Word With Upper Case, even though is is there in the This, as the following is looking only for the word is and not for this. $ grep -iw "is" demo_file THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE. this line is the 1st lower case line in this file. Two lines above this line is empty. And this is the last line. 6. Displaying lines before/after/around the match using grep -A, -B and -C When doing a grep on a huge file, it may be useful to see some lines after the match. You might feel handy if grep can show you not only the matching lines but also the lines after/before/around the match. Please create the following demo_text file for this example. $ cat demo_text 4. Vim Word Navigation You may want to do several navigation in relation to the words, such as: * * * * * * e E b B w W go go go go go go to to to to to to the the the the the the end of the current word. end of the current WORD. previous (before) word. previous (before) WORD. next word. next WORD.

WORD - WORD consists of a sequence of non-blank characters, separated with white space. word - word consists of a sequence of letters, digits and underscores. Example to show the difference between WORD and word * 192.168.1.1 - single WORD * 192.168.1.1 - seven words. 6.1 Display N lines after match -A is the option which prints the specified N lines after the match as shown below. Syntax: grep -A <N> "string" FILENAME The following example prints the matched line, along with the 3 lines after it. $ grep -A 3 -i "example" demo_text Example to show the difference between WORD and word * 192.168.1.1 - single WORD * 192.168.1.1 - seven words. 6.2 Display N lines before match -B is the option which prints the specified N lines before the match. Syntax: grep -B <N> "string" FILENAME When you had option to show the N lines after match, you have the -B option for the opposite.

$ grep -B 2 "single WORD" demo_text Example to show the difference between WORD and word * 192.168.1.1 - single WORD 6.3 Display N lines around match -C is the option which prints the specified N lines before the match. In some occasion you might want the match to be appeared with the lines from both the side. This options shows N lines in both the side(before & after) of match. $ grep -C 2 "Example" demo_text word - word consists of a sequence of letters, digits and underscores. Example to show the difference between WORD and word * 192.168.1.1 - single WORD 7. Highlighting the search using GREP_OPTIONS As grep prints out lines from the file by the pattern / string you had given, if you wanted it to highlight which part matches the line, then you need to follow the following way. When you do the following export you will get the highlighting of the matched searches. In the following example, it will highlight all the this when you set the GREP_OPTIONS environment variable as shown below. $ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8' $ grep this demo_file this line is the 1st lower case line in this file.

Two lines above this line is empty. And this is the last line. 8. Searching in all files recursively using grep -r When you want to search in all the files under the current directory and its sub directory. -r option is the one which you need to use. The following example will look for the string ramesh in all the files in the current directory and all its subdirectory. $ grep -r "ramesh" * 9. Invert match using grep -v You had different options to show the lines matched, to show the lines before match, and to show the lines after match, and to highlight match. So definitely Youd also want the option -v to do invert match. When you want to display the lines which does not matches the given string/pattern, use the option -v as shown below. This example will display all the lines that did not match the word go. $ grep -v "go" demo_text 4. Vim Word Navigation You may want to do several navigation in relation to the words, such as: WORD - WORD consists of a sequence of non-blank characters, separated with white space. word - word consists of a sequence of letters, digits and underscores. Example to show the difference between WORD and word * 192.168.1.1 - single WORD * 192.168.1.1 - seven words.

10. display the lines which does not matches all the given pattern. Syntax: grep -v -e "pattern" -e "pattern" $ cat test-file.txt a b c d $ grep -v -e "a" -e "b" -e "c" test-file.txt d 11. Counting the number of matches using grep -c When you want to count that how many lines matches the given pattern/string, then use the option -c. Syntax: grep -c "pattern" filename $ grep -c "go" demo_text 6 When you want do find out how many lines matches the pattern $ grep -c this demo_file 3 When you want do find out how many lines that does not match the pattern $ grep -v -c this demo_file 4 12. Display only the file names which matches the given pattern using grep -l

If you want the grep to show out only the file names which matched the given pattern, use the -l (lower-case L) option. When you give multiple files to the grep as input, it displays the names of file which contains the text that matches the pattern, will be very handy when you try to find some notes in your whole directory structure. $ grep -l this demo_* demo_file demo_file1 13. Show only the matched string By default grep will show the line which matches the given pattern/string, but if you want the grep to show out only the matched string of the pattern then use the -o option. It might not be that much useful when you give the string straight forward. But it becomes very useful when you give a regex pattern and trying to see what it matches as $ grep -o "is.*line" demo_file is line is the 1st lower case line is line is is the last line 14. Show the position of match in the line When you want grep to show the position where it matches the pattern in the file, use the following options as Syntax: grep -o -b "pattern" file $ cat temp-file.txt 12345 12345 $ grep -o -b "3" temp-file.txt

2:3 8:3 Note: The output of the grep command above is not the position in the line, it is byte offset of the whole file. 15. Show line number while displaying the output using grep -n To show the line number of file with the line matched. It does 1-based line numbering for each file. Use -n option to utilize this feature. $ grep -n "go" demo_text 5: * e - go to the end of the current word. 6: * E - go to the end of the current WORD. 7: * b - go to the previous (before) word. 8: * B - go to the previous (before) WORD. 9: * w - go to the next word. 10: * W - go to the next WORD. Additional Grep Tutorials

7 Linux Grep OR, Grep AND, Grep NOT Operator Examples Regular Expressions in Grep Command with 10 Examples Part I Advanced Regular Expressions in Grep Command with 10 Examples Part II Search in a *.bz2 file using bzgrep, and *.gz file using zgrep

Awesome Linux Articles Following are few awesome 15 examples articles that you might find helpful.

Linux Crontab: 15 Awesome Cron Job Examples Mommy, I found it! 15 Practical Linux Find Command Examples 15 Examples To Master Linux Command Line History

Unix LS Command: 15 Practical Examples

Share Comment If you enjoyed this article, you might also like..
1. 2.

3.

4.

50 Linux Sysadmin Tutorials 50 Most Frequently Used Linux Commands (With Examples) Top 25 Best Linux Performance Monitoring and Debugging Tools Mommy, I found it! 15 Practical Linux Find Command Examples Linux 101 Hacks 2nd Edition eBook

Awk Introduction 7 Awk Print Examples Advanced Sed Substitution Examples 8 Essential Vim Editor Navigation Fundamentals 25 Most Frequently Used Linux IPTables Rules Examples Turbocharge PuTTY with 12 Powerful AddOns

5.

Tags: File Search Utility, Grep Command, Highlight Search Output, Linux Full-Text Searching, Linux Grep Command, Search File Content, Search Multiple Files { 75 comments read them below or add one }

1 Joao Trindade March 28, 2009 at 3:54 am You have a small glitch: >> 4. Match regular expression in files using grep -i Dont you mean: 4. Match regular expression in files using grep -e The rest of the post is great. 2 Ramesh March 29, 2009 at 12:16 am Joao, Thanks for pointing it out. I have corrected it. Also, we can do REGEX without the option -e as shown in the example #4. From Man Pages: SYNOPSIS grep [options] PATTERN [FILE...] grep [options] [-e PATTERN | -f FILE] [FILE...] -e PATTERN, --regexp=PATTERN Use PATTERN as the pattern; useful to protect patterns beginning with -. 3 dragon March 31, 2009 at 11:26 pm Hi: FYI, tip 14 will be 2:3 8:3 on Ubuntu system. (including the \n character I guess 4 Ramesh March 31, 2009 at 11:44 pm Dragon,

Thanks for pointing it out. Ive corrected it. 5 Francesco Talamona April 26, 2009 at 2:48 am I find very useful the following command, when you have to deal with a very lengthy configuration file full of comments: grep -v -E ^\#|^$ /etc/squid/squid.conf It skips every line beginning with an hash (#) or empty, so you can see at a glance the 15 lines edited out of a +4400 lines text file. BTW interesting topics, great posts 6 albar May 7, 2009 at 7:51 pm help me how to bzgrep : ^C02 but ^C is count as one special character, in this word: data1^C02data2 thanks 7 Ramesh Natarajan May 8, 2009 at 5:51 pm @Francesco Talamona, Thanks a lot for sharing your grep command example. Yes. all those empty lines and comment lines can get very annoying when you do grep. So, it is an excellent idea to hide them in the grep output with your examples. 8 sasikala May 11, 2009 at 9:41 pm @albar, try like this grep \^C02

9 albar May 12, 2009 at 1:18 am @sasikala , i do have try that too, but still got nothing, but it works when ^ and C count as two character thanks 10 SathiyaMoorthy May 12, 2009 at 4:33 am @albar You should type ^C as ctrl-v + ctrl-c in grep as single character as $ grep ^C02 file Dont escape, dont type it as ^ C as two characters. Hope this helps. 11 albar May 12, 2009 at 8:59 pm @sathiya, god bless u all it works thanks 12 Manish Patel May 21, 2009 at 7:00 pm Hi I am trying to exclude the last word of all the line like sync.php, uploads.php, backup.php File text include as below /usr/home/htdocs/drag-and-drop/htdocs.php /usr/home//htdocs/sms/publish/pages/sync.php /usr/home/htdocs/track/backup.php /usr/home/htdocs/smstest/smstest.php /usr/home/htdocs/uploads.php /usr/home/htdocs/017/backup.php

How can I achieve that using grep or sed or awk Also how I can use * wildcard in sed command like to replace *.php to *.txt or any other extension. Thank you in advance. Manish 13 Francesco Talamona May 21, 2009 at 10:36 pm Are you restricted to sed or awk? 1) dirname /usr/home/htdocs/drag-and-drop/htdocs.php /usr/home/htdocs/drag-and-drop 2) rename does what you want 14 Manish Patel May 24, 2009 at 6:55 pm Hi, Those lines are the contents of the text file and I dont want to change the actual directory or the file on server. I want to change the contents of the file where all file file names ending at the line should be removed. So the final file contents should look like this cat filecontenet.txt /usr/home/htdocs/drag-and-drop/ /usr/home//htdocs/sms/publish/pages/ /usr/home/htdocs/track/ /usr/home/htdocs/smstest/ /usr/home/htdocs/ /usr/home/htdocs/ I think rename would not help here in editing file contents.

Thank you Manish 15 SathiyaMoorthy May 24, 2009 at 11:43 pm rev filecontenet.txt | cut -d/ -f2- | rev rev filecontenet.txt > reverses the file and pipes to cut command. cut -d/ -f2- > cuts off the first field ( cuts off last field, as it is reversed ). rev > prints the output given order. 16 P0B0T May 26, 2009 at 11:36 pm Manish, I believe youre looking for the following sed -e s/.php$// filecontenet.txt 17 P0B0T May 26, 2009 at 11:39 pm Sorry, didnt read your requirement carefully. Try this: sed -e s/\/[^/]*.php$/\// filecontenet.txt 18 Manish Patel June 5, 2009 at 5:31 pm Hi Thank you to Sathiya Moorthy and P0B0T. Both solution worked very nicely for me. P0B0T can you explain how your command works for each defined option s/\/[^/]*.php$/\// Thank you Manish

19 mano June 10, 2009 at 3:00 am The above info on grep is really great. I want to search for a string in all the files in the directory and add a $ symbol at the start of the searched line and save in the same file. 20 SathiyaMoorthy June 18, 2009 at 10:49 pm @mano More than using grep for this requirement, you can use sed which is: sed -i s/.*abc.*/$&/ * -i : edit the input file. s/// : substitute the matched pattern with the replacement string. /.*abc.*/ : match the string abc /$&/ : Replace with $ followed by matched string. * : all the files in the current directory. This is one way of satisfying your requirement, there may be other efficient ways. Hope this helps. 21 mano June 19, 2009 at 12:17 am Hi SathiaMoorthy, Thank u so much. it works fine. If I need to search for files in all subdirectories, how should this sed command modified? Thanks in advance. mano 22 SathiyaMoorthy June 27, 2009 at 12:05 am @mano Modification in sed command is not needed.

To search for all files in the subdirectory. find . -type f Execute the command on all those files with -exec. find . -type f -exec sed -i s/.*abc.*/#&/ {} \; But think twice before executing this command, because it will recursively edit all the files. Taking backup before executing this command is wise. Refer the earlier article linux find command examples. 23 Vidya July 1, 2009 at 2:59 am Hi, I want to grep next 3 words in a line from the matching criteria word.. like if the line is This is -g gateway -e enterprise -s server Then I want to grep -g gateway -e enterprise from the line Can you please help me in this case. Here gateway and enterprise value can be anything so need to grep next 3 words starting form -g 24 SathiyaMoorthy July 1, 2009 at 5:58 am @Vidhya $ grep -o -E -g \w+ -e \w+ FILENAME -g gateway -e enterprise Explanation of the above command., -o : only matching ( point 13. ) -E : extended regexp

: indicate end of options \w+ : word 25 Vidya July 1, 2009 at 9:00 am Hi Sathiya, Its not working. It says grep: illegal option o grep: illegal option E Usage: grep -hblcnsviw pattern file . . . I am working on Solaris and setting shell as bash. 26 Amit Agarwal September 21, 2009 at 6:18 am grep version on solaris is little older and as man would show you all these options are not available, so you can try ack (standalone) version which is very powerful and requires only perl to be installed. 27 learner October 7, 2009 at 5:31 am Hi, How to use grep to find lines containing multiple strings ex: line1:Today is oct 7, wednesday. not 8th line2: This is not summer. line3: when is summer? I want to return line2 containing strings not and summer both. Thank You. 28 SathiyaMoorthy October 7, 2009 at 10:41 am @learner There are several ways possible, use the one which you find as appropriate.

$ grep "not.*summer" file1 line2: This is not summer. $ grep "not" file1 | grep "summer" line2: This is not summer. 29 learner October 7, 2009 at 10:56 pm @SathiyaMoorthy Thank You for your very quick reply. My question was not piping and hard coding every string , as i mentioned multiple strings, i was looking for something in likes of grep -F string1 string2 string3 string4 .. stringn filename which returns single occurrence of something like either string1 ,string2,.. stringn or all .., what i wanted was only string1 and string2 and . stringn begin returned. [please note that i will be provided with strings as newline separated strings ,which i don't want to parse again and i have constraint of using grep only] Thank You. 30 Ashish December 1, 2009 at 4:07 pm Hi, I need to sthing like this I have a file containing 400 domainId values seprated by new line ex. domain.txt domain1 domain2 domain3

I have a script that takes each domain and calls an api that returns me an xml. like this for each domain val1 domain1 val2 val3 val4 XXX val1 now i want to spit out the domain name in a file that does not matches domainid value XXX. how can i do it using grep TIA 31 Ashish December 1, 2009 at 4:16 pm Hi, I need to sthing like this I have a file containing 400 domainId values seprated by new line ex. domain.txt domain1 domain2 domain3 I have a script that takes each domain and calls an api that returns me an xml. like this for each domain <tag1>val1</tag1> <domain>domain1</domain> <tag2>val2</tag2> <tag3>val3</tag3> <tag4>val4</tag4> <domainid>XXX</domainid> <tag5>val1</tag5>

now i want to spit out the domain name in a file that does not matches domainid value XXX. how can i do it using grep TIA 32 Varun December 17, 2009 at 7:16 am Hi, The options mentioned in point 6 for displaying the context with A, B, & C does not seem to work on Solaris 10 with both grep & egrep Is there a version of this grep available for Solaris? Thank you, Varun. 33 Jawn Hewz December 21, 2009 at 7:54 pm Does the -b (byte offset) work when greping binary files? I do not get an offset returned when I grep a binary file, but I do when using a text file. I am using grep under Cygwin. 34 fety January 11, 2010 at 3:32 am thanks very much for this tutorial. it is very helpful.. 35 eMancu January 24, 2010 at 12:49 pm Awsome tutorial! Im reading all your blog, its amazing! 36 Raghu Baba January 30, 2010 at 4:44 am Hai.. I want to Parse my file .. Word to Excel .. so tell me some grep & cut commands 37 Jeff Floyd February 1, 2010 at 6:54 pm

Whats the difference between $ grep -c ill memo and $ grep -n ill memo? 38 joeq February 4, 2010 at 3:57 am hi i got 1 problemhow can i find a numbers like 99,000,000.95 in my server database using unix command.. tq 39 abhishek February 21, 2010 at 4:31 am content was very useful 40 Anonymous March 8, 2010 at 4:26 am Hi, Those lines are the contents of the text file and I dont want to change the actual directory or the file on server. I want to change the contents of the file where all file file names ending at the line should be removed. So the final file contents should look like this cat filecontenet.txt /usr/home/htdocs/drag-and-drop/ /usr/home//htdocs/sms/publish/pages/ /usr/home/htdocs/track/ /usr/home/htdocs/smstest/ /usr/home/htdocs/ /usr/home/htdocs/ I think rename would not help here in editing file contents. for this question , awk really helpful with single line command go to the current directory ls -l | grep -v ^d | awk {print $9} > new.txt $9 is the last filed which is filename only when u list with option ls -l ,

new.txt contains only the filenames which you wnated to filter out 41 skipper March 26, 2010 at 5:54 am nice article 42 VIKAS April 4, 2010 at 7:23 pm Excellent stuff, just loved grep -A,B,C options and grep -o xxxx.*yyyy kinda commands. This will help me a lot, I used awk more in my shell scripts, But I have got a new friend grep for some selective printing 43 Sam April 17, 2010 at 3:35 am I have just finish reading this wonderful article. Let me answer this: Whats the difference between $ grep -c ill memo and $ grep -n ill memo? grep -c return the number lines that matched ill in memo. grep -n return the matched lines with line-number as prefix. 44 Chong April 20, 2010 at 3:05 am how to grep a statement contain * from a file at the same time to match the first character too. example the statement in filename profile.txt :Mary stay at uttana*istana with her grandmum current grep statement :grep ^Mary stay at uttana*istana profile.txt result: no row matched the grep statement because of * How to use grep command for the combine condition of statement with * and match the front word?

45 vm April 30, 2010 at 8:24 pm In bash script without using perl, how i can grep a number from a file if there exists a number greater than 80 in that file. 46 palash June 13, 2010 at 12:14 pm grep -c pattern filename returns the number of lines that matches the pattern, even if the pattern occurred for more than one time in any line. Is there any option to know how many times the pattern matched in a file? 47 mathan June 20, 2010 at 10:21 am HI, I am new to linux can you tell me how to exit from grep command. mistakenly i type grep filename But its nothing shown. pls looking for quick reply 48 SathiyaMoorthy July 4, 2010 at 7:20 am @mathan There is nothing like exiting from grep. First argument to grep is taken as PATTERN, not as filename. So as far as i understand it is waiting for input to match. So just exit from it using CTRL+D. 49 Ross Huggett September 16, 2010 at 5:04 am Nice article. Thanks. 50 ec October 6, 2010 at 6:14 pm Hi to all, I just started to learn linux a month ago Can I extract 2 to 6 letter words from a text file using one grep

command only! To mention that each word is on its own line whats the grep command to do this job? I tried any combination of grep and not the result which I am looking for 51 Lou February 24, 2011 at 1:16 pm Is there a way to grep for a word on in a file and return that line plus the next? 52 Francesco Talamona February 26, 2011 at 3:33 am @ Lou: cat testfile.txt first line matching line following line ending line grep matching -B 1 testfile.txt first line matching line 53 abhishek kumar April 19, 2011 at 12:53 pm really to nice and too simple to understand, thats great thank you 54 Nikita April 21, 2011 at 12:06 pm PLEASE HELP ON QUESTION B. You are searching a file for lines that contain US state abbreviations in parentheses. e.g.: (ma),(NH),(Ky), etc. So you decide to match any line containing ( ) with exactly two

characters (not letters) in between. A) What grep will get this done? My Answer> grep ([a-zA-Z][A-Za-z]) file You now notice that some of the lines that the grep from part A matched contain the the string (expired). You want to eliminate these lines from your output, so you decide to pipe your output to another grep. B) What will the new command be? (both greps with the pipe) My Answer > grep v grep | grep ([a-zA-Z] [A-Za-z]) > PLEASE HELP! 55 Francesco Talamona April 22, 2011 at 12:44 pm @Nikita: One step is enough: egrep \([a-zA-Z]{2}\) file 56 suprabhat April 27, 2011 at 3:26 am How to display all lines that have less than 9 character in a file 57 Paul May 16, 2011 at 3:09 pm Im new to linux; was wondering what does \# after the grep command accomplish, as shown in the example below? grep \# input*.txt | awk {print $4} | sort | uniq > output.txt thank you 58 Dinesh May 17, 2011 at 4:03 pm For a given patern like Fri Nov 26 16:04:52 2010 I want to grep for all the lines in a file having the above format. But I have all the values except for the time, that is 16:04:52 , the data I have is

Fri Nov 26 2010 . The file is having 5 years of date with the timestamp as specified above. Please let me know How shall I grep the file to get all the lines on the date Fri Nov 26 2010 . thanks 59 shyam May 29, 2011 at 10:45 pm i have a doubt here i tried to look at output of cmd grep [^A-Z] file.txt this is showing all characters excluding capital letter what does this command actually do 60 shivaraj Patil July 25, 2011 at 11:14 am HI i have a file with this values 100 first line 101 second line 101 102 102 109 now i need a script that can take two lines and find which is greatest 61 sudheer September 10, 2011 at 8:32 pm 1) Use grep (or awk) to output all lines in a given file which contain employee ID numbers. Assume that each employee ID number consists of 1-4 digits followed by two letters: the first is either a W or a S and the second is either a C or a T. ID numbers never start with 0s. Further assume that an employee ID is always proceeded by some type of white space tab, blank, new line etc. However, there might be characters after it, for example punctuation. What to turn in: Turn in three things:

a. A file with the regular expression which can directly be used by grep (or awk) b. A text file which you used to test your regular expression. Make sure that you include valid and invalid employee IDs, have them at the beginning and the end of lines, sentences, etc. c. A second document which re-writes the regular expression in a more human-readable form and explains the purpose of the different components of the regular expression. Also include a short explanation of your test cases. 2) Use grep (or awk) to output all the lines in a given file which contain a decimal number (e.g. a number which includes a decimal point). Decimal numbers do not have leading zeros but they might have trailing zeros. Assume the number is always surrounded by white space. What to turn in: The same three things as above (except, of course, for this problem). 3) Write a regular expression for the valid identifiers in Java. You are allowed to use shortcuts, but need to make sure that you specify exactly what they are (e.g. if you use digit specify that that means 0, 1, 2, 3, .9.) 62 Dinesh September 22, 2011 at 12:43 pm Paul grep \# input*.txt | awk {print $4} | sort | uniq > output.txt Since # is a special character,we are treating # as # by putting backslash infront of that. Noe Greap searches for pattern # in a list of file starting as input and nding a txt and then awk prints the 4th field and sort is doing sorting the 4th field returns from awk and unis is doing uniq operation. 63 Dinesh September 22, 2011 at 12:45 pm

Shyam grep [^A-Z] file.txt Grep will print the lines that does not start with CAPTIAL LETTERS. Using ^ inside the [] will do the work opposite to the pattern what you have been searching for 64 haydarekarrar December 23, 2011 at 4:29 am Just a minor thing the last result line is removed in the example above, this should be the result: $ grep this greptest.txt this line is the 1st lower case line in this file. Two lines above this line is empty. And this is the last line. 65 gotham January 22, 2012 at 4:32 am awesome. thanks a lot. 66 edward January 31, 2012 at 2:20 am For example my data is (file.ave) : MRR 120101000000 UTC+07 AVE 60 STF 150 ASL H 150 300 450 600 750 900 TF 0.0149 0.0515 0.1171 F00 -67.04 F01 -69.27 I use grep as: grep -r MRR *.ave > time_0101.txt. In this case all file goes to time_0101.txt, I have many files and I need each output goes to specific file name. Any idea ? And how to use grep to take F00 and F01 ? If I use grep -r F *.ave, the first line will be taken also because of STF, Thanks for help..

67 shrikant February 14, 2012 at 10:23 am Thank you 68 chinna February 19, 2012 at 8:48 am here my question is in a directory i have 10 files.some files contains size in kbs and some files contains size in mbs..i want display the files only which file size is more than 1 mb could anybudy help to find answer for this question. 69 Anderson Venturini March 14, 2012 at 6:01 am Great post! It was very useful! thanks a lot! 70 Anonymous April 9, 2012 at 3:22 pm To answer Chinna If you have some files showing M, and some K, then your ls command is probably running on a Linux box, and using the -h option. The preferred method would be NOT to use the -h option, and let ls print file sizes in bytes, which means that your script will be able to get the same units+detail for all file sizes listed. Then you can use awk to filter out files where $5 (5th field) is over 1Meg, like this: /bin/ls -l | awk $5>=2^20 If you only want file names, not ls style list, then have awk give you that part of output: /bin/ls -l | awk $5>=2^20{print $NF} Note: this does not like file names with spaces notes for Other postings on this thread: To: Francesco Talamona (who was using grep to remove ##_comments from long config files)

grep -v -E ^\#|^$ /etc/squid/squid.conf You may want to remove comments that do NOT start on the first char of a line, and sed is more useful for that sed s/#.*$//;/^[ ]*$/d /etc/squid/squid.conf This will remove comments from each line, then discard the line if blank, or only spaces remain. ========= Also, where the -B and -A options are described for grep This is for GNU/Linux, and not supported for most Non-Linux boxes. #JETS 71 KHEE April 15, 2012 at 8:39 pm Hi expert, i am new to unix env try to use certain command to help me generate 1 output file as below: input file: A A/I 0.2 0.3 0.8 B B/I 0.6 0.8 0.9 C C 0.8 2.1 6.0 I just want to grep all A B C only, where want to skip the line which have the number together. And my output file pattern is A B C Detc ( A, B,C all are a word). mean i want first,second line, and skip 3rd & 4th lines, thanks for help. 72 krishna April 20, 2012 at 9:15 pm

How do you find using regular expression, characters beginning with and ending with any characters. In AXyz122311Xyslasd22344ssaa Aklsssx@sdddf#4=sadsss kaaAASds How do we get the characters slas out that begins with 11Xy and ends with d223 in UNIX using regular expression? echo $MSG | grep -o (?<=11Xy).+(?=d223)' 73 bala May 10, 2012 at 9:12 am @krishna, echo $MSG | grep -o 11Xy.*d223 74 guru May 22, 2012 at 11:52 am i need answer for the following question using grep, determine count of and print the list of words having uppercase letters when the input sentence is given in command line 75 mitchell fox May 24, 2012 at 7:45 am I know this is a simple grep or grep/sed question: I have a log file with identified errors.The errors will lines containing the words Patient ID: 12345678999 (quote marks are mine). i substituted the number 12345678999 for a real number. The real number will be any combination of 11 integers. So I would want to grep for Patient ID: 12345678999 but just end up with the numbers themselves, not the rest of the lines. Example of result I would like: 12345678999 23678900456 Thanks

1. List Currently Running Processes (ps -ef, ps -aux)

Its a commonly used example with a ps command to list down all the process which are currently running in a machine. The following example shows the options of ps command to get all the processes. $ ps -ef root 26551 5 [pdflush] root 26570 5 [pdflush] root 30344 3382 sshd: root@pts/14 root 30365 30344 -bash root 30393 3382 sshd: root@pts/15 Where:

0 Feb10 ? 0 Feb10 ? 0 Feb21 ? 0 Feb21 pts/14 0 Feb21 ?

00:03:41 00:00:20 00:00:11 00:00:02 00:00:10

-e to display all the processes. -f to display full format listing.

In case of BSD machines, you can use ps -aux will give the details about all the process as shown above. $ ps -aux 2. List the Process based on the UID and Commands (ps -u, ps -C) Use -u option to displays the process that belongs to a specific username. When you have multiple username, separate them using a comma. The example below displays all the process that are owned by user wwwrun, or postfix. $ ps -f -u wwwrun,postfix UID PID PPID C STIME TTY CMD postfix 7457 7435 0 Mar09 ? qmgr -l -t fifo -u TIME 00:00:00

wwwrun 7495 7491 0 Mar09 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf wwwrun 7496 7491 0 Mar09 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf wwwrun 7497 7491 0 Mar09 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf wwwrun 7498 7491 0 Mar09 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf wwwrun 7499 7491 0 Mar09 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf wwwrun 10078 7491 0 Mar09 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf wwwrun 10082 7491 0 Mar09 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf postfix 15677 7435 0 22:23 pickup -l -t fifo -u

? ? ? ? ? ? ? ?

00:00:00 00:00:00 00:00:00 00:00:00 00:00:00 00:00:00 00:00:00 00:00:00

Often ps is used with grep like ps -aux | grep command to get the list of process with the given command. But ps command itself has an option to achieve the same. The following example shows that all the processes which has tatad.pl in its command execution. $ ps -f -C tatad.pl UID PID PPID C CMD root 9576 1 0 /opt/tata/perl/bin/perl root 9577 9576 0 /opt/tata/perl/bin/perl STIME TTY TIME

Mar09 ? 00:00:00 /opt/tata/bin/tatad.pl Mar09 ? 00:00:00 /opt/tata/bin/tatad.pl

root 9579 9576 0 /opt/tata/perl/bin/perl root 9580 9576 0 /opt/tata/perl/bin/perl root 9581 9576 0 /opt/tata/perl/bin/perl root 9582 9576 0 /opt/tata/perl/bin/perl root 12133 9576 0 /opt/tata/perl/bin/perl

Mar09 ? 00:00:00 /opt/tata/bin/tatad.pl Mar09 ? 00:00:00 /opt/tata/bin/tatad.pl Mar09 ? 00:00:00 /opt/tata/bin/tatad.pl Mar09 ? 00:00:00 /opt/tata/bin/tatad.pl Mar09 ? 00:00:00 /opt/tata/bin/tatad.pl

Note: We can create aliases for ps command to list processes based on commands, users or groups. 3. List the processes based on PIDs or PPIDs (ps -p, ps ppid) Each process will be assigned with the unique Process ID (PID). When you launch some application, it might fork number of processes and each sub process will have its own PID. So, each process will have its own process id and parent processid. For all the processes that a process forks will have the same PPID (parent process identifier). The following method is used to get a list of processes with a particular PPID. $ ps -f --ppid 9576 UID PID PPID C CMD root 9577 9576 0 /opt/tata/perl/bin/perl root 9579 9576 0 /opt/tata/perl/bin/perl root 9580 9576 0 /opt/tata/perl/bin/perl root 9581 9576 0 /opt/tata/perl/bin/perl root 9582 9576 0 /opt/tata/perl/bin/perl STIME TTY TIME

Mar09 ? 00:00:00 /opt/tata/bin/tatad.pl Mar09 ? 00:00:00 /opt/tata/bin/tatad.pl Mar09 ? 00:00:00 /opt/tata/bin/tatad.pl Mar09 ? 00:00:00 /opt/tata/bin/tatad.pl Mar09 ? 00:00:00 /opt/tata/bin/tatad.pl

root 12133 9576 0 Mar09 ? 00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl The following example is to list the processes which has given PID. $ ps -f -p 25009,7258,2426 UID PID PPID C STIME CMD root 2426 4 0 Mar09 [reiserfs/0] root 7258 1 0 Mar09 /usr/sbin/nscd postfix 25009 7435 0 00:02 pickup -l -t fifo -u TTY ? ? ? TIME 00:00:00 00:00:00 00:00:00

4. List Processes in a Hierarchy (ps forest) The example below display the process Id and commands in a hierarchy. forest is an argument to ps command which displays ASCII art of process tree. From this tree, we can identify which is the parent process and the child processes it forked in a recursive manner. $ ps -e -o pid,args --forest 468 \_ sshd: root@pts/7 514 | \_ -bash 17484 \_ sshd: root@pts/11 17513 | \_ -bash 24004 | \_ vi ./790310__11117/journal 15513 \_ sshd: root@pts/1 15522 | \_ -bash 4280 \_ sshd: root@pts/5 4302 | \_ -bash Note: You can also use tree and pstree command to displays process in a nice tree structure. 5. List elapsed wall time for processes (ps -o pid,etime=)

If you want the get the elapsed time for the processes which are currently running ps command provides etime which provides the elapsed time since the process was started, in the form [[dd-]hh:]mm:ss. The below command displays the elapsed time for the process IDs 1 (init) and process id 29675. For example 10-22:13:29 in the output represents the process init is running for 10days, 22hours,13 minutes and 29seconds. Since init process starts during the system startup, this time will be same as the output of the uptime command. # ps -p 1,29675 -o pid,etime= PID 1 10-22:13:29 29675 1-02:58:46 6. List all threads for a particular process (ps -L) You can get a list of threads for the processes. When a process hangs, we might need to identify the list of threads running for a particular process as shown below. $ ps -C java -L -o pid,tid,pcpu,state,nlwp,args PID TID %CPU S NLWP COMMAND 16992 16992 0.0 S 15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_l ib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend= n,address=5006 16992 16993 0.0 S 15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_l ib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend= n,address=5006 16992 16994 0.0 S 15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_l ib -Xdebug -Xnoagent -Djava.compiler=NONE

-Xrunjdwp:transport=dt_socket,server=y,suspend= n,address=5006 16992 16995 0.0 S 15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_l ib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend= n,address=5006 16992 16996 0.0 S 15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_l ib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend= n,address=5006 16992 16997 0.0 S 15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_l ib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend= n,address=5006 16992 16998 0.0 S 15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_l ib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend= n,address=5006 16992 16999 0.0 S 15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_l ib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend= n,address=5006 16992 17000 0.0 S 15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_l ib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend= n,address=5006 16992 17001 0.0 S 15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_l ib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend= n,address=5006

16992 17002 0.0 S 15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_l ib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend= n,address=5006 16992 17003 0.0 S 15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_l ib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend= n,address=5006 16992 17024 0.0 S 15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_l ib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend= n,address=5006 16992 15753 0.0 S 15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_l ib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend= n,address=5006 16992 15754 0.0 S 15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_l ib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend= n,address=5006 -L option is used to display the list of threads for a process which has the command given. And it also displays nlwp, which represents number of light weight processes. In the above example, a total of 15 java threads are running. 7. Finding memory Leak (ps sort pmem) A memory leak, technically, is an ever-increasing usage of memory by an application. With common desktop applications, this may go unnoticed, because a process typically frees any memory it has used when you close the application.

However, In the client/server model, memory leakage is a serious issue, because applications are expected to be available 247. Applications must not continue to increase their memory usage indefinitely, because this can cause serious issues. To monitor such memory leaks, we can use the following commands. $ ps aux --sort pmem USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 1520 508 ? S 2005 1:27 init inst 1309 0.0 0.4 344308 33048 ? S 2005 1:55 agnt (idle) inst 2919 0.0 0.4 345580 37368 ? S 2005 20:02 agnt (idle) inst 24594 0.0 0.4 345068 36960 ? S 2005 15:45 agnt (idle) root 27645 0.0 14.4 1231288 1183976 ? S 2005 3:01 /TaskServer/bin/./wrapper-linuxx86-32 In the above ps command, sort option outputs the highest %MEM at bottom. Just note down the PID for the highest %MEM usage. Then use ps command to view all the details about this process id, and monitor the change over time. You had to manually repeat ir or put it as a cron to a file. $ ps ev --pid=27645 PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND 27645 ? S 3:01 0 25 1231262 1183976 14.4 /TaskServer/bin/./wrapper-linux-x86-32 $ ps ev --pid=27645 PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND 27645 ? S 3:01 0 25 1231262 1183976 14.4 /TaskServer/bin/./wrapper-linux-x86-32

Note: In the above output, if RSS (resident set size, in KB) increases over time (so would %MEM), it may indicate a memory leak in the application. UNIX Basic commands: ps The ps command displays active processes. The syntax for the ps command is: ps [options] Options: -a -c -d -e -f Displays all processes on a terminal, with the exception of group leaders. Displays scheduler data. Displays all processes with the exception of session leaders. Displays all processes. Displays a full listing.

glis Displays data for the list of group leader IDs. t -j -l Displays the process group ID and session ID. Displays a long listing

plis Displays data for the list of process IDs. t

slis Displays data for the list of session leader IDs. t Displays data for the list of terminals. tlist ulis Displays data for the list of usernames. t Examples: ps -ef ps -aux

Вам также может понравиться