- Resources
- Find
- Search
- Processes
- File permissions
- List files
- Symlinks
- Checking logs
- Text manipulation
- Sed
- Networking
- Space
- Miscellaneous
find dir -type f
Find all files in the directoryfind dir -type d
File all directories within this directoryfind dir -name “a*.mp3” -print0
Find all files/directory with name like a***.mp3find dir -type f -name “a*.txt”
Find all files (recursively) with name like a***.txtfind dir -type f -print0 | xargs -0 grep -l "word"
List file names with matching wordfind dir -type f -print0 | xargs -0 mv -t
Move all files from dir tree into single directory
grep “word” file
All matching lines in the filegrep -i “word” file
All matching lines - Ignore casegrep -r “word” dir
All matching lines in (recursively) all files or directorygrep -c “word” file
Count of matching linesgrep “word” file | wc -l
Count of matching linesgrep -v “word” file | wc -l
Count of non-matching linesgrep -o -w “word” file | wc -w
Word countgrep -l -r “word” dir
List file names with matching wordfind dir -type f -print0 | xargs -0 grep -l "word"
List file names with matching word
nohup program.sh > mylog.txt 2>&1 &
&
starts process as child process in the backgroundnohup
Ctrl-C or terminating session sends kill signal to all child processes,nohup
catches and ignores it>
redirect output (from left/source to right/target)2
error stream1
output stream2>&1
append all errors to output
top
Check CPU and memory consumptionkill -9 processId
kill the process with that pidkill -3 processId
for a JVM process, will write thread dump to output streamps -ef | grep word
find process with matching wordecho $?
print output of last process
chmod file
Changechmod +x file
Add execute permissionchmod +w file
Add write permissionchmod +r file
Add read permissionchmod 755 file
Change permissionchmod -R 755 dir
Change permission recursivelychmod g+w file
Add write permission at group level- Numbers: 1=x, 2=w, 3=w+x, 4=r, 5=r+x, 6=r+w, 7=r+w+x
- References: u=user, g=group, o=others, a=all
ls
list files in this directoryls -ltr
list files in this directory with more details and in reverse chronological orderls -ltra
same as above + list hidden files (names starting with.
)ls -ltr | grep "^l"
Lists all symlinks (ls -ltr
lists symlinks withl
as first character of output)
- Symbolic or soft links. Pointer to a file.
ln -s /java/jdk7 jdk
create symlink jdk which points to/java/jdk7
ln -ns /java/jdk8 jdk
update symlink jdk to point to/java/jdk8
tail -f logfile
output appended data as file grows (f=follow)less +F logfile
open file, and output appended data as file grows
head file
display first 10 lineshead -20 file
display first 20 linestail file
display last 10 linestail -5 file
display last 5 lineshead -20 file | tail -1
display 20th linecut -f3 file
display 3rd field for each line, tab as separatorcut -d',' -f3 file
display 3rd field for each line,,
as separatorcut -c3-6 file
display characters from 3rd to 6th position for each linecut -c7- file
display characters from 7th to end of each line
sed s/unix/linux file
replace first occurrence of "unix with "linux"sed s/unix/linux/3 file
replace 3rd occurrencesed s/unix/linux/g file
replace all occurrencessed s/unix/linux/3g file
replace all occurrences starting from 3rd onesed -i '1 d' file
delete header of filesed -i '$ d' file
delete footer of filesed –n '10 p' file
print 10th line of the file
ping hostname
ortelnet hostname port
check if remote host is alivescp /dir/file user@hostname:/targetdir/file
copy file to different hostnetstat -a | grep "port"
check host connected to machine's port
df -h
space in current drive (-h
is human readable format)du -h .
sizes of all files in current directorydu -sh .
sizes of current directorydu /bin/* | sort -n
sort all files based on size (asc)
lsof file
list processes using the filerev file
reverses the text in each lineecho "string" | rev
reverses the stringsort file
sorts file lines alphabeticallysort -n file
sorts file lines numericallyuniq file
filter out adjacent duplicate lines and printuniq -c file
print with count at the begining of each line of outputuniq -d file
print only duplicates