Linux Command File, Dir, Search

Author avatar wrote on 10/06/2021

Linux Command File, Dir, Search

The find command is one of the most powerful tools in the Linux system administrators arsenal. It searches for files and directories in a directory hierarchy based on a user given expression and can perform user-specified action on each matched file.

You can use the find command to search for files and directories based on their permissions, type, date, ownership, size, and more. It can also be combined with other tools such as grep or sed.

The general syntax for the find command is as follows:

find [options] [path…] [expression]

  • The options attribute controls the treatment of the symbolic links, debugging options, and optimization method.
  • The path… attribute defines the starting directory or directories where find will search the files.
  • The expression attribute is made up of options, search patterns, and actions separated by operators.

To search for files in a directory, the user invoking the find command needs to have read permissions on that directory.


Search for a specific pattern in a file with:
grep [pattern] [file_name]

Recursively search for a pattern in a directory:
grep -r [pattern] [directory_name]

Find all files and directories related to a particular name:
locate [name]

List names that begin with a specified character [a] in a specified location [/folder/location] by using the find command:
find [/folder/location] -name [a]

See files larger than a specified size [+100M] in a folder:
find [/folder/location] -size [+100M]

File Commands
List files in the directory:
ls

List all files (shows hidden files):
ls -a

Show directory you are currently working in:
pwd

Create a new directory:
mkdir [directory]

Remove a file:
rm [file_name]

Remove a directory recursively:
rm -r [directory_name]

Recursively remove a directory without requiring confirmation:
rm -rf [directory_name]

Copy the contents of one file to another file:
cp [file_name1] [file_name2]

Recursively copy the contents of one file to a second file:
cp -r [directory_name1] [directory_name2]

Rename [file_name1] to [file_name2] with the command:
mv [file_name1] [file_name2]

Create a symbolic link to a file:
ln -s /path/to/[file_name] [link_name]

Create a new file:
touch [file_name]

Show the contents of a file:
more [file_name]

or use the cat command:
cat [file_name]

Append file contents to another file:
cat [file_name1]  [file_name2]

Display the first 10 lines of a file with:
head [file_name]

Show the last 10 lines of a file:
tail [file_name]

Encrypt a file:
gpg -c [file_name]

Decrypt a file:
gpg [file_name.gpg]

Show the number of words, lines, and bytes in a file:
wc


-->