The place where the Information Matters

Common Unix Commands with useful syntax

0

Copy(cp) Command Examples:

  1. To Copy a file from one directory to another : $cp file.doc new/directory/folder
  2. To Make a copy of a file in the current directory as newfile.doc: $cp file.doc newfile.doc
  3. To Copy multiple files in a directory into another directory: $ cp main.doc demo.doc new.doc Folder/backup
  4. To Copy everything from a directory to a new directory: $cp * home/new/directory
  5. To copy a directory, including all its files and subdirectories, to another directory: $ cp -R * /home/new/backup
  6. To copy all the files with extension .txt into another directory: $ cp **/*.txt /abc/imp_files/folder1/

Move(mv) Command Examples:

  1. To move a file from one directory to another: $mv myfile.txt myfiles/folder
  2. To move multiple files from one directory to another: $mv myfile1 myfile2 myfiles/folder
  3. To move all the file with .txt extension to another directory: $ mv *.txt myfiles/folder
  4. To move all the files in subdirectory ‘folder’ to a current directory ‘myfiles’: $mv folder/* .
  5. To move only the newer file than the destination file: $mv -u myfile.txt myfiles/folder
  6. How to move all files and folders from one directory to another: $ mv /path/sourcefolder/* /path/destinationfolder/
  7. How to move all files and folders from one directory to another including hidden files: $mv /sourcedir/{,.[^.]}* /destdir/
  8. To rename the file from file1.txt to file2.txt: $mv file1.txt file2
  9. To rename the directory name from directory1 to directory 2: $mv directory1 directory2
  10. To take a backup of an existing file after overwriting a file: $mv -b file1.txt file2.txt

           ls after this command will give file2.txt file2.txt ~

  1. To move a file(file1) to another directory (directory2) and give it a new name(file2): $mv file1 directory1/file2
  2. To forcefully move a file to a directory: $mv -f file1 /directory1
  3. To prompt for a confirmation before overwriting any files: $mv -i file 1 file2
  4. To change the file extension from *.txt to *.text for all the files in a directory:

           for x in *.txt

           do

           mv “$x” “`basename ‘$x’ .txt`.text”

           done

Sed Command Examples:

  1. To change all the occurrences of ‘old’ to ‘new’ in a file named finalreport.txt: $sed ‘s/old/new/g’ finalreport.txt
  2. To change all the occurrences of ‘old’ to ‘new’ in a directory’s all .txt files: $sed -i -e ‘s/old/new/g’ *.txt
  3. To change all the occurrences of ‘old’ to ‘new’ in a file named finalreport.txt and save the result in a new file named finalreport_updated.txt: $sed ‘s/old/new/g’ finalreport.txt > finalreportupdated.txt
  4. To filter the lines in a file to see on the ones with “John” in them: $sed -n ‘John/p’ finalreport.txt
  5. To filter the lines in a file to see on the ones with “John” in them and save the output in a new file: $sed -n ‘John/p’ finalreport.txt > finalreportupdated.txt
  6. To replace the nth occurrence of a pattern in a line: $sed ‘s/unix/linux/2’ finalreport.txt
  7. To replace from nth occurrence to all occurrence in a line: $sed ‘s/unix/linux/3g’ finalreport.txt
  8. To parenthesize first character of each word: $ echo “Welcome To Jungle” | sed ‘s/\(\b[A-Z]\)/\(\1\)/g’

             Output will look like: (W)elcome (T)o (J)ungle

  1. To replace a string on a specific line number (3) only: $sed ‘3 s/unix/linux/’ finalreport.txt
  2. To print only the replaced lines: $sed -n ‘s/old/new/p’ finalreport.txt
  3. To replace a string on a range of line (line 1 to 3): $sed ‘1,3 s/old/new/’ finalreport.txt
  4. To DELETE a particular line (5th line) in a file: $ sed ‘5d’ finalreport.txt
  5. To delete the last line of a file: $ sed ‘$d’ finalreport.txt
  6. To delete the lines from a range (line 4 to line 6): $ sed ‘4,6d’ finalreport.txt
  7. To delete the lines from 3rd to last : $ sed ‘3,$d’ finalreport.txt
  8. To delete pattern(abc) matching line: $ sed ‘/abc/d’ finalreport.txt

Echo Command Examples:

  1. To print string “Hello, World!” on console: $ echo “Hello, World!”
  2. To print value of x, where x=10 : $ echo $x
  3. To print a string without space: $ echo -e ‘My \bname \bis \bsam \bsmith.’

        ‘\b’ backspace with backslash interpreter ‘-e’ removes all the spaces in between

        Output: Mynameissamsmith.

  1. To print a string with each word in a new line: $ echo -e ‘My \nname \nis \nsam \nsmith.’

       ‘\n’ – New line with backspace interpreter ‘-e’ treats new line from where it is used.

         Output: My

                      name

                      is

                      sam

                      smith.

  1. To print a string with a tab in between the words: $ echo -e ‘My \tname \tis \tsam \tsmith.’

       ‘\t’ – horizontal tab with backspace interpreter ‘-e’ to have horizontal tab spaces.

       Output: My        name    is             sam        smith.

  1. To print a string with vertical tab: $ echo -e ‘My \vname \vis \vsam \vsmith.’

        ‘\v’ – vertical tab with backspace interpreter ‘-e’ to have vertical tab spaces.

       Output: My

                         name

                                  is

                                     sam

                                            smith.

  1. To remove before part and display an only as part of the syntax: $ echo -e ‘My \rname \is \sam \smith.’

        ‘\r’ – carriage return with backspace interpreter ‘-e’ to have specified carriage return in output

        Output: name is sam smith.

  1. To remove after part and display an only the before part of the syntax: $ echo -e ‘My \name \cis \sam \smith.’

       ‘\c’ – suppress trailing new line with backspace interpreter ‘-e’ to continue without emitting new line.

       Output: My name is

  1. To display the result of echo in a new file: $echo “Hello World!” > newfile.txt

Remove (rm) Command Examples:

  1. To remove the file: $ rm myfile.txt
  2. To forcefully delete a file even if it is protected: $ rm -f myfile.txt
  3. To remove all the files in directory: $rm *
  4. To forcefully remove all the files in a directory even if there are protected files: $rm -f *
  5. To remove every file in the working directory, but prompt before each file to confirm: $rm -i *
  6. To remove every file in the working directory; prompt for confirmation if more than three files are being deleted: $rm -I *
  7. To remove a directory and the files it contains and prompt to make action: $rm -r mydirectory
  8. To remove a directory and the files it contains and not prompt to make action: $rm -rf mydirectory
  9. To remove multiple files at once: $rm file1.txt file2.txt
  10. To remove all the file with particular extension(.txt) in a directory: $ rm *.txt
  11. To remove all the file with particular extension(.txt) in a directory with a action for confirmation: $ rm -i *.txt
  12. To display the information after the rm command makes its action: $rm -v file.txt
  13. To remove multiple files based on a pattern. Here removing a file from current_directory that have been modified in the last day: $ find ./current_directory -type f -mtime -1 -exec rm -i {} \;
  14. To remove multiple files with particular naming convention: $rm -f finalreport123* OR                                        $ find ./ -name “finalreport123” -exec rm -rf {} \;

Grep Command Examples:

  1. To find a particular pattern(eg. error) in a file: $grep “error” newfile.log
  2. To find the particular pattern no matter of any case (eg. error, ERROR, Error): $grep -I “error” newfile.log
  3. To search for a pattern in multiple files(file1, file2: $grep “error file*.*
  4. To search for a pattern as a regular expression (starts with ‘error’ and end with ‘failure’ and anything in between: $grep “error.*failure” newfile.log
  5. To display the line numbers which contains the matched pattern in a file: $grep -n “error*” newfile.log
  6. To highlight the search using the grep: $grep -color “error” newfile.log
  7. To list all the lines of the file which doesn’t contain a particular pattern: $grep -v “error” newfile.log
  8. To display all the lines in a file which has a particular line in the beginning: $grep ˄error newfile.log
  9. To display all the lines in a file which has a particular line in the end: $grep error$ newfile.log
  10. To give the count of a particular pattern occurring in a file: $grep -c “error” newfile.txt

Find Command Examples:

  1. To find a file by its name: $find . -name “error” -print
  2. To find and report all.txt files: $find . -name \*.txt -print
  3. To find all the files in directory 1 and directory2 larger than 2000 blocks (about 1,000KB) and that have not been accessed in over 30 days: $ find /directory1 /directory2 -size +2000 -atime +30 -print
  4. To remove (with prompting) all files starting in the /mydir directory that have not been accessed in over 100 days: $ find /mydir -atime +100 -ok rm {} \;
  5. To show a long listing starting in /mydir of files not modified in over 20 days or not accessed in over 40 days:   $ find /mydir \(-mtime +20 -o -atime +40\) -exec ls -l {} \;
  6. To list and remove all regular files named ‘error’ starting in the directory /newfolder that are larger than 500KB: $ find /newfolder -type f -size +1000 -print -name error -exec rm {} \;
  7. To find and replace: $ find ./ -type f -exec sed -i ‘s/oldword/newword/g’ {} \;
  8. To search for a text within multiple files: $ find ./ -type f -name “*.md” -exec grep ‘error’ {} \;
  9. To find the files whose name are not “abc.txt”: $ find -not -name “abc.txt”
  10. To find the empty files in a directory: $ find . -maxdepth 1 -empty
  11. To find the largest file in the current directory and sub directories: $ find . -type f -exec ls -s {} \; | sort -n -r | head -1
  12. To find the smallest file in the current directory and sub directories: $ find . -type f -exec ls -s {} \; | sort -n -r | tail -1
  13. To find files smaller than 10M size: $ find . -size -10M
  14. To find the files based on the file permissions (777): $find . -perm 777
  15. To find the files which are modified 1 day back: $ find . -not -mtime -1
  16. To delete the files which contains the name “error”: $ find -name “*error*” -exec rm -r {} \;

SCP Command Examples:

  1. To copy the file “myfile.txt” from a remote host to the local host:                                                                          $scp [email protected]:myfile.txt /some/local/directory
  2. To copy the file “myfile.txt” from the local host to a remote host:                                                                       $scp myfile.txt your_username@HostInfo:/some/remote/directory
  3. To Copy the directory “temp” from the local host to a remote host’s directory “myfolder”:                                $scp -r temp your_username@HostInfo:/some/remote/directory/myfolder
  4. To Copy the file “myfile.txt” from remote host “RemoteHostInfo1″ to remote host ” RemoteHostInfo2″:                  $scp your_username@RemoteHostInfo1:/some/remote/directory/myfile.txt \ your_username@ RemoteHostInfo2:/some/remote/directory/
  5. To Copy the files “myfile.txt” and “newfile.txt” from the local host to your home directory on the remote host: $scp myfile.txt newfile.txt your_username@HostInfo:~
  6. To Copy multiple files from the remote host to your current directory on the local host:                                    $scp your_username@HostInfo:/some/remote/directory/\{a,b,c\} .

Disclaimer:

This article is written solely for the information purpose. The author has collected information from various websites for some of the answers and some of the answers (Not All) might be a direct copy and paste.

The article was collected and written by Bijaya Subedi. Bijaya is currently working as an ETL tester in a DC/VA based Mortgage Company.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.