Linux - Snippets

- - posted in linux, snippets | Comments

Search all the files/folder with that name and remove them

1
 find . -name "FILE-TO-FIND"-exec rm -rf {} \;

Find all files having .bak (*.bak) extension in current directory and remove them:

1
  find . -type f -name "*.bak" -exec rm -f {} \;

Find all core files and remove them

1
  find / -name core -exec rm -f {} \;

Find all *.bak files in current directory and removes them with confirmation from user:

1
  find . -type f -name "*.bak" -exec rm -i {} \;

Find all pdf files and copy them in DestinationFolder:

1
  find . -type f -name "*.pdf" -exec cp {} DestinationPath \;

Searching


Search for pattern in files

1
   grep pattern files

Search recursively for pattern in dir

1
   grep -r pattern dir

Search for pattern in the output of command

1
   command | grep pattern

Locate file – Find all instances of file
Starting with the root directory, look for the file called filename

1
   find / -name filename

Starting with the root directory, look for the file containing the string filename

1
   find / -name ”*filename*”

Starting with the directory called dir, look for and list all files containing TextStringToFind

1
   grep TextStringToFind /dir

Create symbolic link link to file

1
   ln -s file link

Create or update file

1
   touch file

Places standard input into file

1
   cat > file

Display the file called file one page at a time, proceed to next page using the spacebar

1
   more file

Output the first 10 lines of file

1
   head file

Display the first 20 lines of the file called file

1
   head -20 file

Output the last 10 lines of file

1
   tail file

Display the last 20 lines of the file called file

1
   tail -20 file

Output the contents of file as it grows, starting with the last 10 lines

1
   tail -f file

Compression

Compression

Create a tar named file.tar containing files

1
   tar cf file.tar files

Extract the files from file.tar

1
   tar xf file.tar

Create a tar with Gzip compression

1
   tar czf file.tar.gz files

Extract a tar using Gzip

1
   tar xzf file.tar.gz

Create a tar with Bzip2 compression

1
   tar cjf file.tar.bz2

Extract a tar using Bzip2

1
   tar xjf file.tar.bz2

Compresses file and renames it to file.gz

1
   gzip file

Decompresses file.gz back to file

1
   gzip -d file.gz

Comments