Linux commands to find files

Categorías: 

Etiquetas: 

Find text inside all files in a folder recursively with grep command

grep -iRHn "searchText" path_to_folder

Concatenate another grep command to exclude some files, e.g: svn files

grep -iRHn "searchText" path_to_folder | grep -v ".svn" > save_ocurrences_in_a_file.txt

Find any files in a folder and sort them by last updated

find path_to_folder -type f -printf '%T+\t%p\n' | sort -n 

Find files of a certain user with users write permissions and without group write permissions

find path_to_folder -user user_name -type f -perm /u+w ! -perm /g+w

Concatenate another command to add write permissions

find path_to_folder -user user_name -type f -perm /u+w ! -perm /g+w -exec chmod g+w {} \;

Search recursively symbolic links

find path_to_folder -type l -exec ls -lad {} \;

Find zip files older than 5 days and delete them

find -name '*.zip' -type f -mtime +5 -exec rm -f {} \;

Find .svn files and delete them

find path_to_folder -name .svn -exec rm -R '{}' \;

Search recursively a folder and set permissions for web apps (644 for files, 755 for folders)

find path_to_folder -type f -exec chmod 644 {} \; && find path_to_folder -type d -exec chmod 755 {} \;

Find files by size, show the top 10

du -hsx * | sort -rh | head -10
  • du command : Estimate file space usage.
  • sort command : Sort lines of text files or given input data.
  • head command : Output the first part of files i.e. to display first 10 largest file.