Useful Linux Commands

1. find command used to searches files or directories in entire file system or in current directory 

find . -name *.java  //searches all files with extension .java in current directory
find / -name Sample -type f  // "-type f" option used to return only files
find / -name Sample -type d // "-type d" option used to return only directories
find /usr -name "Sample*" -type f // searches through the /usr directory for all files that begin with the letters Sample, followed by anything else
find . -type d -name build // to find all directories named build under the current directory
find . -type f -name "Foo*" -exec rm {} \; // find all files in the current directory that begin with the letters 'Foo' and delete them
find . -iname foo // Search for all files and directories named foo, FOO, or any other combination of uppercase and lowercase characters in the current directory
find . -mtime -7 // To find all files and directories that have been modified in the last seven days
find . -mtime -7 -type f // To find all files that have been modified in the last seven days
find . -mtime -7 -type d // To find all directories that have been modified in the last seven days
find /opt /usr /var -name sample.sh -type f // Searches "samples.sh" file in multiple directories

2. locate command is used for search files and directories

locate tomcat.sh // search the entire file system for 'tomcat.sh' (uses the locate database)
locate -i spring.jar // case-insensitive search

3. grep command is used for search strings

grep 'joe' * // Search for string in multiple files
grep -i score sample.txt // case-insensitive file searching
grep -n we 
sample.txt // To show the line numbers of the files that match your grep command, just add the -n option
grep -inR "text" .  // 
case-insensitive file searching in current directory
grep 'fred' /etc/passwd // search for lines containing 'fred' in /etc/passwd
grep fred /etc/passwd // quotes usually not when you don't use regex patterns
grep -B5 "the living" sample.txt  //show all matches, and five lines before each match
grep -A10 "the living" sample.txt  // show all matches, and ten lines after each match
grep -B5 -A5 "the living" 
sample.txt  //five lines before and ten lines after
grep -n we 
sample.txt  // show line numbers as well as the matching lines 
grep '^fred' /etc/passwd  //find 'fred', but only at the start of a line
grep '[FG]oo' *  // find Foo or Goo in all files in the current dirgrep '[0-9][0-9][0-9]' *  // find all lines in all files in the current dir with three numbers in a row
egrep 'apple|banana|orange' *  // search for multiple patterns, all files in current dir
egrep -i 'apple|banana|orange' * // same thing, case-insensitive
egrep 'score|nation|liberty|equal' sample.txt  // all lines matching multiple patterns
egrep 'score|nation|liberty|equal' gettysburg-address.txt // This Unix egrep command searches the file named gettysburg-address.txt for the four strings shown (score, nation, liberty, and equal). It returns any lines from the file that contain any of those words.

4. cat command is used to view text files

cat -n myfile.txt // Showing line numbers
cat /etc/passwd // to view the contents of the /etc/passwd file on your Unix/Linux system
cat sample.txt // display the content of the file

5. wc command is used for count

$ wc /etc/passwd // to count the number of lines, words, and characters in a file
        65 185 3667 /etc/passwd

$ wc -l /etc/passwd // If you just want to know the number of lines in a file just add the -l argument
       65 /etc/passwd

$ wc -w MyStory.txt // if you want to know the number of words in a file, add the -w argument, like this
      185 MyStory.txt

ps -e | wc -l // shows the number of processes currently running on your Linux system


6. rm command is used for removing files or directories

rm oldfile.txt // to remove one file
rm file1 file2 file3  // to delete multiple Linux files at one time
rm -i files file2 file3 // The -i stands for "inquire", so when you use this option the rm command prompts you with a yes/no prompt before actually deletingrm -r OldDirectory // To delete Linux directories with the rm command, you have to specify the -r optionrm -r Directory1 Directory2 Directory3 // delete multiple directories at one timerm *.html // to delete all HTML files in the current directoryrm index* // where I'm deleting all files in the current directory that begin with the string "index"


7. mv command to rename or move Linux files and directories

mv Chapter1 Chapter1.old
mv MyStuff /tmp
// if you have a directory named MyStuff, you can move it to the /tmp directory
mv file1 file2 file3 /tmp // where three files are moved to the /tmp directory
mv file1 .. // if you want to move it up one level in the directory hierarchy

8. pwd to get current directory path

  $ pwd
    /home/techchai/

9. mkdir command is used for creating directories

mkdir tmp // creates a new directory named tmp in your current directory
mkdir memos letters e-mail // create multiple directories at one time
mkdir -p /home/joe/customer/acme/foo/bar // to create several sub directories at one time

10. ls command is used to list files and directories
 
ls -al // show all the files in cyrrent directory

11. tar command is used to create tar files

tar cvf MyProject.20090816.tar MyProject // tar command is to create an archive of a subdirectory.

c means “create archive.”
v means “verbose,” which tells tar to print all the filenames as they are added to the archive.
f tells tar that the name of the archive appears next (right after these options).


tar tvf my-archive.tar // List the contents of a tar archivetar tzvf my-archive.tgz //To list all the files in a compressed archive, add the z flag
tar xvf my-archive.tar // Extracting a tar archive
tar xzvf my-archive.tar.gz // For compressed archives the tar extract command

tar czvf MyProject.20090816.tgz MyProject // You can compress a tar archive with the gzip command after you create it

12. top command is an interactive utility that displays a character-based screen of all processes running on the current system

13. who command lets you display the users that are currently logged into your Unix computer system
     $ who

14. ps command by itself, it only shows very basic information about the processes you are currently running
     $ps

15. df command stands for "disk free". It is meant to show Linux disk space information, including disk space that is used, disk space remaining, and how filesystems are mounted on your Linux (or Unix) system
     $ df
       Filesystem 1K-blocks Used Available Use% Mounted on
       /dev/vzfs 10485760 5713424 4772336 55% /


16. du command shows disk usage information about only the files and directories you specify

17. lsof command lists information about files that are open by processes running on the system. (The lsof command itself stands for “list of open files.”)

18. chmod command name stands for "change mode", and as that name implies, the chmod command is used to change the mode of Unix/Linux files.

chmod +x myShellScript.sh // is commonly used to make a file "executable"
chmod +r myfile.txt // use of the chmod command is to make a file "readable". If a file doesn't have read permission, you can add read permission to that file
chmod +w myfile.txt // In the same way, you can also give a file write permission

19. cp command to copy files and directories on Linux systems

cp source destination
 // This cp command copies the original file named source to the new file named destination. After issuing this command both your original file and the new file will be in the current directory.
cp -r Foo /tmp // You can also copy directories with the Linux cp command, and when you do this, you just need to remember to use the -r option

20. uname // Stands for Unix name - print detailed information about the machine name, Operating System and Kernel.

root@techchai:~# uname -a
Linux techchai 3.8.0-19-generic #30-Ubuntu SMP Wed May 1 16:36:13 UTC 2013 i686 i686 i686 GNU/Linux


21. history command is used to prints the history of long list of executed commands in terminal.

1 comment: