Collection of useful Unix commands


– How to count all directories?
find . -maxdepth 1 -type d | wc -l

– How to search and replace in file?
find comet/appconf/* -type f -exec sed -i ’s/WhatYouSEARCHFor/WhatYouReplaceFor/g' {} \;
— escaped http: http:\/\/

– How to search and delete files?
find . -name "FILE-TO-FIND" -exec rm -rf {} \;

– How to search in file?
grep -rnw comet/appconf/* -e “WhatYouWantToSearch”
find . | xargs grep 'WhatYouWantToSearch' -sl

– How to add “new” line to the end of a file?
for file in $(find ./* -iname *.properties); do echo 'hello' >> $file; done

– How to copy fast files with windows?
rsync -auvr /Users/walid/Desktop/1/* /Users/walid/Desktop/2/

– How to sort Files by size?
du --max-depth=1 . | sort -n –r


– How can you display Linux users?
sudo cat /etc/passwd | cut -d":" -f1

– How to show the pid for certain ports?
Mac: lsof -n -i:8080
Linux: netstat | grep 8080