ShellScripts Flashcards
My system is running very slow. What do i do ?
top command to show CPU utilization per core
How many files are there in my current directory?
ls -lrt to see if . and .. come up and then accordingly ls -lrt|wc -l
I am getting an out of space email in one of my mounts, what do i do ?
du -ah |sort -rh|head -n 20 to see top 20 files
I want to see how much space is left in a directory
df -ah
My directory has multiple sub directories, but i want to show much space is used only for my first 2 directories
du -ah –max-depth=2
I want to find all files which contain the line “Published items “
grep ‘Published’|grep -v 0
I need to find a file named pwd.txt and i cant remember where it was. How do find it’s location?
find / -type f -name pwd.txt
I don’t like the default command prompt, and want to change it to show the hostname/current directory
PS1=”[\h \w]”
How do I see status of my last command ?
echo $? #0 means success
How do i force delete all files under a directory ?
rm -rf
How do i run a long running command so that its not killed ?
nohup &
nohup will keep running until it’s done, even if the user that started it logs out.
& to run a process to the background
NO HANGUP - nohup
If i dont want to see a command’s error output , what do i do?
2>/dev/null
How do i append a line to a file?
echo “line”»_space; filename. #> to insert
How do i put a comment in a shell script
Use # like a shebang script
How do i find PID of a process named mysql ?
ps -ef|grep mysql|cut -f4 -d” “ # 4th column separated by space
How do you find all processes started by a user ?
-u
You have a directory contqaining many files and directories.
You want to search for a line say “CRIT”. How will you search it ?
#cd grep -r CRIT .
How to search for a tag in binary files ?
grep -a
How to show only filenames ?
grep -l
How to read compressed files in a RO shell
zgrep
I want to shutdown/decommission a host. I want to see which files are still being written on the server.
find . -mtime -7 -ls. #-ls to give more details
If my system is showing OoM exceptions, what do i do ?
Can check free memory by free -h, then if more memory is available, can provide more memory to the script