Skip to content

Instantly share code, notes, and snippets.

@qmmr
Last active September 22, 2018 13:40
Show Gist options
  • Save qmmr/5403337 to your computer and use it in GitHub Desktop.
Save qmmr/5403337 to your computer and use it in GitHub Desktop.
UNIX - useful terminal commands

read content of file.txt and replace all text with text2 and put it into file2.txt

cat file.txt | sed s/text/text2/ > file2.txt

scp /path/to/local/file username@hostname:/path/to/copy/to/

scp username@hostname:/path/to/remote/file /local/path/to/copy/to/

search for case-insensitive index.php in current dir

find ./ -iname "index.php"

search all files (but .svn) and look for string myFunction

grep -iR myFunction ./ --exclude-dir=.svn

find files with pattern

find -name "*._*"

remove all files that you find in directory that match regexp

rm -f `find ./ | grep "^\.\_.*"`

As for bulk processing, with find's -exec option or the xargs command, you can apply arbitrary commands to the files find finds.

replace "foo" with "bar" in all C source & header files

$ find . -name '*.[ch]' -a -exec sed -i s/foo/bar/ {} \;

find total size of all html files

$ find . -name '*.html' -a -exec ls -kl {} \; | awk -v total=0 ' { total += $5 } END { print total } ' 

archive C source & header files

find . -name '*.[hc]' | xargs tar cyf src.tar.bz2

find's -delete lets you, well, delete found files:

find . -name '._*' -a -delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment