Basic Linux Tutorial
Introduction – What is it? Why to learn? Linux installation directory structures Boot process Run levels in Linux Desktop Environments Different shells BASH Internal and External Commands Basic Linux Commands Important files and directories in Linux Environmental and Shell Variables Command history in Linux Character classes in Linux Text editors vim nano Searching files Creating new files Viewing File Contents File commands File permissions and ownership WildCards (Globbing) in files File compression Directory commands xargs command in Linux Comparing files Searching patterns using grep command Translating the characters using tr command Extracting data using cut command Stream editing using sed command Data extraction and reporting using awk command Sorting the file or string input uniq command in Linux Difference between grep, tr, cut, sed and awk commands Hardware commands Hard disk and memory space commands Working with Processes Managing Jobs Working with cron jobs Service command in Linux Network commands Managing Users and Groups Other Popular commands Standard streams and Redirection Pipes Package Managers in LinuxSearching files and directories in linux shell
We can search files (and directories) using 4 commands.- find
- locate
- whereis
- which (only binaries)
find Command
Syntax of find command is given below.
$ find path_where_to_find nameOfFile
Examples –Finding files by name
To find the files and directories with exact name, you can use below command.
find . -name “f1.txt”
To find the files and directories with containing specific word
find . -name “*xyz*”
To find only directories containing specific word, you can use below command
find . -name “*xyz*” -type d
To find only files containing specific word, you can use below command
find . -name “*xyz*” -type f
To find only files containing specific word, you can use below command
find . -name “*xyz*” -type f
To find all files and directories ending with .txt, you can use below command in Case Insensitive manner. Here -iname stands for case insensitive.
find . -iname “*.txt”
To find all files and directories with name f1 only in current directory (Not sub directories), you can use below command. Here “-maxdepth” indicates the directory level.
find . -maxdepth 1 -name “f1”
Operators in find command- find . -not -name “abc” : This command will search all files except with name “abc”
- find . ! -name “abc”: This command is same as above command.
- find -name ‘*.php’ -o -name ‘*.txt’ : This command will find the files where name ends with “.php” or “.txt”
Finding file using regular expression
$ find ./ -regex ‘.*f1.*’
Finding file based on user or group
Below command will find the files and directories in current directory and sub-directories where the user of the file or directory is paul.
$ find . -user paul
Below command will find the files and directories in current directory and sub-directories where the group of the file or directory is dev.
$ find . -group dev
Finding file based on access (a), modification (m) and change(c) in attributes time
atime stands for files accessed in 24*N Hours (or in N days). Same applies to mtime and ctime. amin stands for files accessed in N minutes. Same applies to mmin and cmin. N can be prefixed with +, – or nothing.- +N means find the files before N
- -N means find the files between N and now
- N means find the files exactly on N
$ find ./mydir -mmin -10
Below command will find the files and directories in directory “mydir” and sub-directories where the files were modified on exactly 10th minute in the past.
find ./mydir -mmin 10
Below command will find the files and directories in directory “mydir” and sub-directories where the files were modified before 10th minute.
find ./mydir -mmin +10
Below command will find the files and directories in directory “mydir” and sub-directories where the files were modified between 20 and 40 days in the past.
find ./mydir -mtime +20 –mtime -40
Finding file based on permissions and attributes
- find . -perm /a=x : This command will find files and directories with execute permission for current user.
- find . -perm /u=r : This command will find files and directories with read only permission for current user.
- find . -perm 0664 : This command will find files and directories with specific permission for current user.
Finding file based on size
- find . -type f -size +20M : This command will find all files with size >= 20 MB
- find . -empty : This command is used to find empty files and directories
- find . -size +2M -size -5M : This command will find files between 2MB and 5MB
Performing bulk operation on files
We can also perform the operations on files found by “find” command using below syntax.
find . -type f -exec ls -l {} ;
find . -type f | xargs ls -l
Main difference between -exec option and xargs is that when you use -exec option, the specified command is executed for each file. So if find command finds 3 files, “ls” command is executed 3 times. But in case of xargs, “ls” command is executed only once. xargs will invoke “ls” command only once with names of the found files as arguments. So xargs is much faster as compared to -exec option.
locate Command
# The syntax of the locate command is given below.
$ locate <fileName>
“locate” command searches for a file in the database called as mlocate (/var/lib/mlocate/mlocate.db). It is recommeded to update the database before using locate command. You can use below command to update the mlocate database.
$ updatedb
whereis Command
The syntax of the whereis command is given below.
$ whereis fileName
which Command
The syntax of which command is given below. Note that fileName should be executable binary.
$ which fileName
Web development and Automation testing
solutions delivered!!