Basic Linux Commands
pwd - displays information about the current location in the file system (the path of the directory (folder) you are in);
dir, ls - shows a list of files and folders in the current directory;
cd - (change the current directory) allows you to move to another folder;
cd ../ - will move to a folder on a higher level;
cd foo - will go to the child folder "foo";
cd /var - will go to the "var" folder located in the root of the file system;
touch fine_name - create a new file named "fine_name";
mkdir dir_name - create a new folder "dir_name" in the current directory;
rm file_name - delete the file "file_name";
rm -r dir_name - delete the folder "dir_name";
cp origin_name new_name - copy files and folders;
mv old_name new_name - move files and folders;
ln -s origin_name link_name - create a symbolic link;
Search by Content
To search for a file by content, use this command:
grep -rnw 'path' -e 'some text'
Use the l modifier to display only a list of matching files:
grep -rnwl 'path' -e 'some text'
If there are no files, run the command only with the r or rl parameter:
grep -rl 'path' -e 'some text'
Search by Name
To find a file by name, run the following command:
find -name file.txt
Search by Size
To find a file by size, run this command:
find ./ -xdev -type f -size +5M
Search by Date
To find the file by modification date (number of days since modification) you need to use this command:
find -type f -mtime -7
Archiving and Unzipping
To archive and unzip files or folders, we recommend using the tar command instead of zip, unzip.
The command is:
tar -cvzf archive_name.tar.gz path1 path2 --exclude="path1/subfolder"
It allows you to create a .tar.gz archive file from files and folders. You can specify more than one path (directory or file) you want to archive. You can also specify the path you do not want to be archived in the --exclude parameter.
If you do not want several paths to be archived, write several --exclude parameters, one after the other.
tar -cvzf archive_name.tar.gz path1 --exclude="path1/subfolder" --exclude="path1/db.sq"
In the tar command, the cvzf options stand for:
с (--create) - create a new archive;
v (--verbose) - display information about the archiving progress. You do not have to specify this parameter if you do not want to receive detailed information about the archiving process;;
z (--gzip) - use file compression. If you do not want the files to be compressed in the archive, do not use this parameter.
To unzip the .tar.gz archive, run the following command:
tar -xvzf archive_name.tar.gz
Working with a Database Dump
To create a database dump (backup), run this command:
mysqldump -uDB_USER_NAME -hDB_HOST -pDB_USER_PASSWORD DB_NAME > DUPM_FILE.sql
-u - indicates the name of the database user. Set its value (WITHOUT SPACEBAR!) after the -u parameter, the same applies to other parameters;
-h - indicates the host of the database server (database ip). If the database server is on the same server (localhost or 127.0.0.1), you don't have to specify this option;
-p - allows you to specify a database user password.
We recommend: don't specify a user password in the command for security reasons. Use the -p parameter without a value, and the system will require the password;
DB_NAME - the name of the database to be dumped;
DUPM_FILE.sql - file which database dump will be added to;
To import a dump into a database, use this command:
mysql -uDB_USER_NAME -hDB_HOST -p DB_NAME < DUPM_FILE.sql
In this example, we did not specify a password in the command.
Notice the difference between the characters ">" and "<" in the mysqldump, mysql commands.
Changing Access Rights and Owner
To change access rights for files and folders, use the chmod command. For example:
chmod 644 file.txt
chmod 755 folder1
chmod -R 755 folder2
The -R parameter allows you to apply changes recursively.
Use the chown command to change the owner or group.
chown username:groupname file.txt
chown username:groupname folder1
chown -R username:groupname folder2
Display information about the used memory on the disk
Run the df -h command to view disk space data.
Run the du -sh ./* command to find information about the size of files and folders in the current directory.