Useful Commands
Some basic, but useful commands
File & folder ownership & groups
Find all files/folders with user apache.
find . -user apache
Find all files/folders that DO NOT have user apache.
find . ! -user apache
Change owner and/or group on a file and/or folder.
chown owner.group file.txt
Find all files with user apache and change owner and/or group.
find . -user apache -exec chown owner.group {} \;
Changing file & directory folder permissions
Find all files with 777 permissions and change to 644.
find . -perm 777 -type f | xargs chmod 644
Change all file permissions to 644 recursively.
find . -type f -exec chmod 644 {} \;
Find all directory folders with 777 permissions and change to 755.
find . -perm 777 -type d | xargs chmod 755
Change all directory folder permissions to 755 recursively.
find . -type d -exec chmod 755 {} \;
Move/rename a file or directory folder
Move a file to a different directory
mv myfile.txt /path/to/different/directory
Rename a file in same directory
mv myfile.txt mynewfile.txt
Backup/copy a file or directory folder
Copy a file ‘myfile.txt’ to different directory
cp myfile.txt /path/to/different/directory
Copy a directory ‘mydirectory’ and it’s content to a new directory ‘mydirectory_backup’.
cp -avr mydirectory mydirectory_backup
Copy the contents inside a directory ‘mydirectory’ to a different directory on same server. Note only the contents of the directory will be copied, not the actually directory. The location/destination directory must exist before running the command.
cp -avr mydirectory/* /path/to/different/directory
Editing a file in VIM
Open a file in VIM
vim myfile.txt
Go to line eg:196. Hit enter after you enter the loine number
:196
Search for string/pattern from where the cursor is in the file eg: keyword
/keyword
Repeat search forward in the file
n
Repeat search backwards in the file
N
Enter insert mode (to begin editing)
i
Quit a file without saving (you must hit ‘Esc’ on your keyboard first)
:q!
Save/write and quit a file (you must hit ‘Esc’ on your keyboard first)
:wq!