Saturday, 22 June 2013

FIND and grep Command

Q: Find all files owned by user1, and copy it to the /root/user2 directory while perserving the permission


  1. first find how many required files are available of for user1
  2. find / -user user1
  3. mkdir /root/user2
  4. find /home -user user1 -exec cp -p {} /root/user2 \;
  5. ls -al /root/user2 (to verify all files are available)
  6. or run find / -user user1 again, it will show files available in /root/user2 as well


Note:  '-p' preserves the user/group/chmod permissions

Q: To redirect the contents of /etc/passwd to another file

#cat /etc/passwd > /user/test

#grep root /user/test | sed 's/root/ROOT/g' | sort > /home/user/file1.txt

No comments:

Post a Comment