Wednesday 7 November 2012

Linux for you Admin Tips: 9


1. find all files owned by a particular user in Linux



Find file owned by group

Use following syntax:
find directory-location -group {group-name} -name {file-name}
Where,
  • directory-location : Try to find files in this directory path
  • -group { group-name} : Find file belongs to group-name.
  • -name {file-name} : Actual name of file to search
For example, locate or find all file belongs to group "ftpusers" in /home directory, enter:
# find /home -group ftpusers
Find all *.c file belongs to group "ftpusers" in /data/project directory:
# find /data/project -group ftpusers -iname "*.c"

Find file owned by user

Use following syntax:
find directory-location -user {username} -name {file-name}
Where,
  • directory-location : Try to find files in this directory path
  • -user { group-name } : Find file belongs to username.
  • -name {file-name} : Actual name of file to search
For example, locate or find all file belongs to user vivek in /var directory:
# find /var -user ajay
Find all *.pl files file belongs to user vivek in /var/www directory
# find /var/www -user ajay -type -f -name "*.pl"


2. Linux- find and remove files with one find command


Linux or UNIX - Find and remove file syntax

To remove multiple files such as *.jpg or *.sh with one command find, use
find . -name "FILE-TO-FIND"-exec rm -rf {} \;
OR
find . -type f -name "FILE-TO-FIND" -exec rm -f {} \;
The only difference between above two syntax is that first command can remove directories as well where second command only removes files.

(a) Find all files having .bak (*.bak) extension in current directory and remove them:

$ find . -type f -name "*.bak" -exec rm -f {} \;
(b) Find all core files and remove them:
# find / -name core -exec rm -f {} \;
(c) Find all *.bak files in current directory and removes them with confirmation from user:
$ find . -type f -name "*.bak" -exec rm -i {} \;
Output:
rm: remove regular empty file `./data0002.bak'? y rm: remove regular empty file `./d234234234fsdf.bak'? y rm: remove regular empty file `./backup-20-10-2005.bak'? n
Caution: Before removing file makes sure, you have backup of all-important files. Do not use rm command as root user it can do critical damage to Linux/Unix system.

No comments:

Post a Comment