Common ssh commands
This is a list of commonly used SSH commands that I used to keep in a file on my computer, while managing the server I open this file often so I am thinking others might need this too. For more advanced commands like compressing and decompressing multi-part archives, I will create separate articles.
To delete an entire directory and its files
rm -r -f YourDirectory
Obviously you should use extreme caution and do not remove files you need.
Get the file size in MB
du -m photos.zip
Get the complete directory size
du -sh .
List files with size and details next to them
Count total files in a directory
ls -1 targetdir | wc -l
Replace "targetdir" with a dot to count files or folders in current directory.
Search for a string in all files
grep -r -l "whatever" *
"whatever" is the string to look for, and * tells it to look in all files in current directory, or specify a directory like this: grep -r -l "whatever" /var
Change the file owner and group of files
chown -R adrian:psacln *
Where "adrian" is the user of the files and psacln is the group. If you don't know what name and group to use, then do a "sh -alh" and look at your other file's owner and group name.
When not able to read/write files, bad file owner might be the cause rather than permission code (0777, 0755, etc)
Change permission code of a file
chmod 0777 my_file.txt
Changes the file permission codes, 0777 makes a file writable by all, other permission codes are 0755, etc. If you have trouble reading/writing a file make sure that file owner and group is the same with yout other "normal" files that can be read/written by you.
Creating and extracting zip files
I recommend using zip files when you need to move sites to another server, zipping the files works well if site is under 1GB, and transfering one large file will be much faster than using FTP for each file in site. For sites/backups over 1GB I you should use multi-volume .tar archives that need few more commands.
To create a zip of current directory you are in (note the "*"), use this command, note that the ".htaccess" tells it to also load .htaccess file from current directory, otherwise it might be skipped:
zip -r documents.zip * .htaccess
To test the created zip file:
zip -T documents.zip
To unpack the zip file:
Export and import large database
These commands will allow you to quickly move databases between servers, or create local backups of your database
Export a database to an .sql file (backup.sql):
mysqldump --add-drop-table -h site.com -u username -p dbname > backup.sql
Where you will need to replace site.com with your domain name ("localhost" also works), also replace database username and database name; iIt will ASK for password AFTER entering the command !!!
Then to import this file into a database:
mysql -h site.com -u username -p dbname < backup.sql
Tip: you can move files between servers with wget command:
but make sure you do not leave db your files online for everyone to read
Get the CPU details of your server
This would probably be useful for dedicated servers and virtual dedicated servers, it tells you processor model, cache size, speed, etc