Copy(cp) Command Examples:
- To Copy a file from one directory to another : $cp file.doc new/directory/folder
- To Make a copy of a file in the current directory as newfile.doc: $cp file.doc newfile.doc
- To Copy multiple files in a directory into another directory: $ cp main.doc demo.doc new.doc Folder/backup
- To Copy everything from a directory to a new directory: $cp * home/new/directory
- To copy a directory, including all its files and subdirectories, to another directory: $ cp -R * /home/new/backup
- To copy all the files with extension .txt into another directory: $ cp **/*.txt /abc/imp_files/folder1/
Move(mv) Command Examples:
- To move a file from one directory to another: $mv myfile.txt myfiles/folder
- To move multiple files from one directory to another: $mv myfile1 myfile2 myfiles/folder
- To move all the file with .txt extension to another directory: $ mv *.txt myfiles/folder
- To move all the files in subdirectory ‘folder’ to a current directory ‘myfiles’: $mv folder/* .
- To move only the newer file than the destination file: $mv -u myfile.txt myfiles/folder
- How to move all files and folders from one directory to another: $ mv /path/sourcefolder/* /path/destinationfolder/
- How to move all files and folders from one directory to another including hidden files: $mv /sourcedir/{,.[^.]}* /destdir/
- To rename the file from file1.txt to file2.txt: $mv file1.txt file2
- To rename the directory name from directory1 to directory 2: $mv directory1 directory2
- 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 ~
- To move a file(file1) to another directory (directory2) and give it a new name(file2): $mv file1 directory1/file2
- To forcefully move a file to a directory: $mv -f file1 /directory1
- To prompt for a confirmation before overwriting any files: $mv -i file 1 file2
- 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:
- To change all the occurrences of ‘old’ to ‘new’ in a file named finalreport.txt: $sed ‘s/old/new/g’ finalreport.txt
- To change all the occurrences of ‘old’ to ‘new’ in a directory’s all .txt files: $sed -i -e ‘s/old/new/g’ *.txt
- 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
- To filter the lines in a file to see on the ones with “John” in them: $sed -n ‘John/p’ finalreport.txt
- 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
- To replace the nth occurrence of a pattern in a line: $sed ‘s/unix/linux/2’ finalreport.txt
- To replace from nth occurrence to all occurrence in a line: $sed ‘s/unix/linux/3g’ finalreport.txt
- 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
- To replace a string on a specific line number (3) only: $sed ‘3 s/unix/linux/’ finalreport.txt
- To print only the replaced lines: $sed -n ‘s/old/new/p’ finalreport.txt
- To replace a string on a range of line (line 1 to 3): $sed ‘1,3 s/old/new/’ finalreport.txt
- To DELETE a particular line (5th line) in a file: $ sed ‘5d’ finalreport.txt
- To delete the last line of a file: $ sed ‘$d’ finalreport.txt
- To delete the lines from a range (line 4 to line 6): $ sed ‘4,6d’ finalreport.txt
- To delete the lines from 3rd to last : $ sed ‘3,$d’ finalreport.txt
- To delete pattern(abc) matching line: $ sed ‘/abc/d’ finalreport.txt
Echo Command Examples:
- To print string “Hello, World!” on console: $ echo “Hello, World!”
- To print value of x, where x=10 : $ echo $x
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
- To display the result of echo in a new file: $echo “Hello World!” > newfile.txt
Remove (rm) Command Examples:
- To remove the file: $ rm myfile.txt
- To forcefully delete a file even if it is protected: $ rm -f myfile.txt
- To remove all the files in directory: $rm *
- To forcefully remove all the files in a directory even if there are protected files: $rm -f *
- To remove every file in the working directory, but prompt before each file to confirm: $rm -i *
- To remove every file in the working directory; prompt for confirmation if more than three files are being deleted: $rm -I *
- To remove a directory and the files it contains and prompt to make action: $rm -r mydirectory
- To remove a directory and the files it contains and not prompt to make action: $rm -rf mydirectory
- To remove multiple files at once: $rm file1.txt file2.txt
- To remove all the file with particular extension(.txt) in a directory: $ rm *.txt
- To remove all the file with particular extension(.txt) in a directory with a action for confirmation: $ rm -i *.txt
- To display the information after the rm command makes its action: $rm -v file.txt
- 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 {} \;
- To remove multiple files with particular naming convention: $rm -f finalreport123* OR $ find ./ -name “finalreport123” -exec rm -rf {} \;
Grep Command Examples:
- To find a particular pattern(eg. error) in a file: $grep “error” newfile.log
- To find the particular pattern no matter of any case (eg. error, ERROR, Error): $grep -I “error” newfile.log
- To search for a pattern in multiple files(file1, file2: $grep “error file*.*
- 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
- To display the line numbers which contains the matched pattern in a file: $grep -n “error*” newfile.log
- To highlight the search using the grep: $grep -color “error” newfile.log
- To list all the lines of the file which doesn’t contain a particular pattern: $grep -v “error” newfile.log
- To display all the lines in a file which has a particular line in the beginning: $grep ˄error newfile.log
- To display all the lines in a file which has a particular line in the end: $grep error$ newfile.log
- To give the count of a particular pattern occurring in a file: $grep -c “error” newfile.txt
Find Command Examples:
- To find a file by its name: $find . -name “error” -print
- To find and report all.txt files: $find . -name \*.txt -print
- 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
- 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 {} \;
- 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 {} \;
- 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 {} \;
- To find and replace: $ find ./ -type f -exec sed -i ‘s/oldword/newword/g’ {} \;
- To search for a text within multiple files: $ find ./ -type f -name “*.md” -exec grep ‘error’ {} \;
- To find the files whose name are not “abc.txt”: $ find -not -name “abc.txt”
- To find the empty files in a directory: $ find . -maxdepth 1 -empty
- To find the largest file in the current directory and sub directories: $ find . -type f -exec ls -s {} \; | sort -n -r | head -1
- To find the smallest file in the current directory and sub directories: $ find . -type f -exec ls -s {} \; | sort -n -r | tail -1
- To find files smaller than 10M size: $ find . -size -10M
- To find the files based on the file permissions (777): $find . -perm 777
- To find the files which are modified 1 day back: $ find . -not -mtime -1
- To delete the files which contains the name “error”: $ find -name “*error*” -exec rm -r {} \;
SCP Command Examples:
- To copy the file “myfile.txt” from a remote host to the local host: $scp [email protected]:myfile.txt /some/local/directory
- To copy the file “myfile.txt” from the local host to a remote host: $scp myfile.txt your_username@HostInfo:/some/remote/directory
- 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
- 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/
- 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:~
- 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.