Thursday, May 27, 2010

find command





1.file sharing between linux machine and window machine.
-sudo apt-get install samba-client
-smb://172.28.6.205
done!


Find tips

Find and Copy Files:
$find ~/downloads/ -iname "*.mp3" -exec cp {} "/home/user1/Desktop/" \;

1.‎ ‏find ~/downloads/ -iname "*.mp3" finds all files ending in‭ ‬.mp3 in‭ ‬/home/user/downloads folder recursively.
‎ 2. -exec starts the action.
‎ 3. cp is the command we want to run, in this case, copy mp3 files.
‎ 4. After {} is where the filename will go when find finds a file (if blank takes
the current directory).
‎ 5. \; is the required ending sequence of -exec.

Find and Change Permission:
$find‎ ‏/home‎ –‏iname‭ "*‬.sh‭" –‬exec chmod‭ ‬+x‭ {} \;

This command will finds all files ending in‭ ‬.sh in‭ ‬/home recursively & ‭add execute permission.

Find and delete files
$find ~/downloads/ -iname "*.mp3" -exec rm -i {} \; (Delete all the mp3 files in downloads folder)

Find all the files greater than certain size‭:

$find / -type f -size +100M (The following command will list all the files in the system greater than 100MB)

$find ~ -name '*.txt' -size 4k -exec ls -l {} \; (The following command will list all txt files in the system of 4KB)

Delete all the archive files with extension‭ *‬.tar.gz & greater than‭ ‬100MB‭:
Please be careful while executing the following command as you don’t want to delete the files by mistake.‭ ‬The best practice is to execute the same command with ls‭ –‬l to make sure you know which files will get deleted when you execute the command with rm.

#‎ ‏find‭ ‬/‭ ‬-type f‭ ‬-name‭ *‬.tar.gz‭ ‬-size‭ ‬+100M‭ ‬-exec ls‭ ‬-l‭ {} \;
#‎ ‏find‭ ‬/‭ ‬-type f‭ ‬-name‭ *‬.tar.gz‭ ‬-size‭ ‬+100M‭ ‬-exec rm‭ ‬-f‭ {} \;

Find files that are not modified in the last x number of days‭:
The following command will list all the files that were modified more than‭ ‬60‭ ‬days ago under the current directory.
#‎ ‏find‭ ‬-mtime‭ ‬+60

Find files that are modified in the last x number of days‭:
The following command will list all the files that were modified in the last two days under the current directory.‭
#‎ ‏find‭ ‬–‬mtime‭ ‬-2

No comments:

Post a Comment