Useful Linux Commands

Submitted by admin on Tue, 08/02/2016 - 19:32

Dealing with crontab

crontab [-u user] [-l | -r | -e] [-i] [-s]
-u  user Specifies the user whose crontab is to be viewed or modified. If this option is not given, crontab opens the crontab of the user who ran crontab. Note: using su to switch users can confuse crontab, so if you are running it inside of su, always use the -u option to avoid ambiguity.
-l Display the current crontab.
-r Remove the current crontab.
-e Edit the current crontab, using the editor specified in the environment variable VISUALor EDITOR.
-i Same as -r, but gives the user a yes/no confirmation prompt before removing the crontab.
-s SELinux only: appends the current SELinux security context string as an MLS_LEVELsetting to the crontab file before editing or replacement occurs. See your SELinux documentation for detailed information.


Restart Mysql on a Linux with Unix Command Line:

### To start ###
service mysqld start
# OR
/etc/init.d/mysqld start
### To stop ###
service mysqld stop
# OR
/etc/init.d/mysqld stop
### To restart ###
service mysqld restart
# OR
/etc/init.d/mysqld restart


Create a Text File using cat command

cat > foo.txt

This is a test.
press CTRL+D to save file

to see its content:

cat foo.txt



Use find to search file names

To find files larger than 100MB:

find . -type f -size +100M -name tables.*
mv $(find . -size +3M) big

other useful flags are :  -maxdepth 1 and -name "*search*"


Search a pattern in all files

grep -rnw '/path/to/directory' -e 'pattern'
  • -r or -R is recursive,
  • -n is line number, and
  • -w stands for match the whole word.
  • -l (lower-case L) can be added to just give the file name of matching files.



copying a directory to another

cp -r /from_directory/ /to_directory/

#copying all files from inside a directory#
cp -r /from_directory/* /to_directory/

to say 'y' to all The prompt feature

most commands work with -i option in the end of commands


Creating a symbolic link (shortcuts)

ln -s source_file myfile

source_file is the destination and  myfile is the name of the link.

files and folders size: 

du -hs *

du -hs * | sort -rh
  1. du command: Estimate file space usage.
  2. -h : Print sizes in human-readable format (e.g., 10MB).
  3. -S : Do not include the size of subdirectories.
  4. -s : Display only a total for each argument.
  5. sort command: Sort lines of text files.
  6. -r : Reverse the result of comparisons.
  7. -h : Compare human-readable numbers (e.g., 2K, 1G).



Changing user properties in Linux

for changing the username and user ID or any property of a user in Linux we can use the usermod command as follows:

#changing username
usermod -l new-name old-name

#changing user ID
usermod -u new-UID username

#listing all users and groups info
grep admin /etc/passwd
grep admin /etc/group
#result : 

When changing the user ID, the users home directory and all files will automatically be updated to the new UID.

502 is the uid and 503 is the group id of user admin.


Changing file owner and group

chown -R admin:foo /home/meysam

the command above changes recursively all files and directories in /home/meysam  and sets owner to admin and group to foo.



Changing ssh configuration

you can find ssh configuration file in etc/ssh/ directory called sshd_config. you can edit this file using any editor and change the Port, PermitRootLogin, PubkeyAuthentication and other things.

.ssh/config file

Host dev
    Port 22000
    User fooey
    IdentityFile ~/.ssh/github.key

you can edit the .ssh/config file and save hosts for better use of ssh. after that you can connect to servers like below:

ssh dev




Basic searching:

  • /pattern - search forward for pattern
  • ?pattern - search backward
  • n or * - repeat forward search
  • N or #- repeat backward