My scrapbook about anything which I learned or want to remember, Sometime about tech tips, thoughts and rambling. If you find anything useful don't forget to give thumbs-up :)

Breaking

Monday, October 26, 2015

Linux One Liners #3




# www.datagenx.net
## 1. How to list the files that are accessed 5 days ago in the current directory?
find -atime 5 -type f
## 2. How to list the files that were modified 5 days ago in the current directory?
find -mtime 5 -type f
## 3. How to list the files whose status is changed 5 days ago in the current directory?
find -ctime 5 -type f
## 4. How to replace the character '/' with ',' in a file?
sed 's/\//,/' < filename
sed 's|/|,|' < filename
## 5. Write a command to find the number of files in a directory.
ls -l|grep '^-'|wc -l
## 6. Write a command to display your name 100 times.
The Yes utility can be used to repeatedly output a line with the specified string or 'y'.
yes <your_name> | head -100
## 7. Write a command to display the first 10 characters from each line of a file?
cut -c -10 filename
## 8. The fields in each line are delimited by comma. Write a command to display third field from each line of a file?
cut -d',' -f2 filename
## 9. Write a command to print the fields from 10 to 20 from each line of a file?
cut -d',' -f10-20 filename
## 10. Write a command to print the first 5 fields from each line?
cut -d',' -f-5 filename
## 11. By default the cut command displays the entire line if there is no delimiter in it. Which cut option is used to supress these kind of lines?
The -s option is used to supress the lines that do not contain the delimiter.
## 12. Write a command to replace the word "bad" with "good" in file?
sed s/bad/good/ < filename
## 13. Write a command to replace the word "bad" with "good" globally in a file?
sed s/bad/good/g < filename
## 14. Write a command to replace the word "apple" with "(apple)" in a file?
sed s/apple/(&)/ < filename
## 15. Write a command to switch the two consecutive words "apple" and "mango" in a file?
sed 's/\(apple\) \(mango\)/\2 \1/' < filename
## 16. Write a command to display the characters from 10 to 20 from each line of a file?
cut -c 10-20 filename
## 17. Write a command to print the lines that has the the pattern "july" in all the files in a particular directory?
grep july *
This will print all the lines in all files that contain the word “july” along with the file name. If any of the files contain words like "JULY" or "July", the above command would not print those lines.
## 18. Write a command to print the lines that has the word "july" in all the files in a directory and also suppress the filename in the output.
grep -h july *
## 19. Write a command to print the lines that has the word "july" while ignoring the case.
grep -i july *
The option i make the grep command to treat the pattern as case insensitive.
## 20. When you use a single file as input to the grep command to search for a pattern, it won't print the filename in the output. Now write a grep command to print the filename in the output without using the '-H' option.
grep pattern filename /dev/null
The /dev/null or null device is special file that discards the data written to it. So, the /dev/null is always an empty file.
Another way to print the filename is using the '-H' option. The grep command for this is
grep -H pattern filename
## 21. Write a command to print the file names in a directory that does not contain the word "july"?
grep -L july *
The '-L' option makes the grep command to print the filenames that do not contain the specified pattern.
## 22. Write a command to print the line numbers along with the line that has the word "july"?
grep -n july filename
The '-n' option is used to print the line numbers in a file. The line numbers start from 1
## 23. Write a command to print the lines that starts with the word "start"?
grep '^start' filename
The '^' symbol specifies the grep command to search for the pattern at the start of the line.
## 24. In the text file, some lines are delimited by colon and some are delimited by space. Write a command to print the third field of each line.
awk '{ if( $0 ~ /:/ ) { FS=":"; } else { FS =" "; } print $3 }' filename
## 25. Write a command to print the line number before each line?
awk '{print NR, $0}' filename
## 26. Write a command to print the second and third line of a file without using NR.
awk 'BEGIN {RS="";FS="\n"} {print $2,$3}' filename
## 27. How to create an alias for the complex command and remove the alias?
The alias utility is used to create the alias for a command. The below command creates alias for ps -aef command.
alias pg='ps -aef'
If you use pg, it will work the same way as ps -aef.
To remove the alias simply use the unalias command as
unalias pg
## 28. Write a command to display todays date in the format of 'yyyy-mm-dd'?
The date command can be used to display todays date with time
date '+%Y-%m-%d'
## 29.For LOOP
## 1. Rename all ".old" files in the current directory to ".bak":
for i in *.old
do
j=`echo $i|sed 's/old/bak/'`
mv $i $j
done
## 2. Change all instances of "yes" to "no" in all ".txt" files in the current directory. Back up the original files to ".bak".
for i in *.txt
do
j=`echo $i|sed 's/txt/bak/'`
mv $i $j
sed 's/yes/no/' $j > $i
done
## 3. Loop thru a text file containing possible file names. If the file is readable, print the first line, otherwise print an error message:
for i in `cat file_list.txt` do if test -r $i
then
echo "Here is the first line of file: $i"
sed 1q $i
else
echo "file $i cannot be open for reading." fi done
## 30. How to replace the word "lite" with "light" from 100th line to last line in a file?
sed '100,$ s/lite/light/' < filename
# www.datagenx.net
view raw LOL3.ksh hosted with ❤ by GitHub

For more - CLICK HERE


Like the below page to get update  
https://www.facebook.com/datastage4you
https://twitter.com/datagenx
https://plus.google.com/+AtulSingh0/posts
https://groups.google.com/forum/#!forum/datagenx

No comments:

Post a Comment

Disclaimer

The postings on this site are my own and don't necessarily represent IBM's or other companies positions, strategies or opinions. All content provided on this blog is for informational purposes and knowledge sharing only.
The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of his information.