Frequently used commands
Some of the most frequently used UNIX / Linux commands.
User
Add user with custom shell and comment
useradd <USER_NAME>-s=User login shell
-c=Comment
$ useradd testuser -s /bin/sh -c "Some Comment"Change user shell
$ chsh -s /bin/bash <USERNAME>List user
$ cat /etc/passwdAdd user to wheel group
$ sudo usermod -G wheel <USER_NAME>
# OR modify the following file
$ sudo vim /etc/group
## File: /etc/group
...
wheel:*:0:root,<USER_NAME>
...Permission/Access Control
Modify directory owner
Use -R(recursive) to include files in directory
chown <USER_NAME>:<GROUP_NAME> <DIRECTORY>
$ chown -R testuser:testgroup dirnameModify access permissions
u=user, g=group, o=others
r=read, w=write, x=execute
+=add permission, -=remove permission
Use comma to separate the multiple permission
chmod <OPTIONS> <MODE> <FILE>
$ chmod u+r,g+x filenameFile
Cleanup file
$ cat /dev/null > <LOG_FILE>Finding top N largest file
$ du -Sh | sort -rh | head -n <N>List open file for specific port
$ lsof -p <PORT> | wc -lSplit file by line
split <OPTIONS> <LINE_NUMBER> <FILE_NAME> <SPLITTED_FILE_NAME>-l=linebumber, -b=bytes
splitted file name will be
<SPLITTED_FILE_NAME>aa<SPLITTED_FILE_NAME>abin the following example
$ split -l 200 filename splittedfilenameSort output and print sizes in human readable form
$ ls -lShSSH
Generate ssh key
$ ssh-keygen -t rsa -C <YOUR_NAME>Untagged
Allow sudo command
modify sshd_config and reload sshd service
$ sudo vim /etc/ssh/sshd_config
## File: /etc/ssh/sshd_config
...
PasswrodAuthentication yes
...
# Reload sshd service
$ sudo service sshd reloadFinding pid with specific port
$ sudo lsof -i :80 | grep LISTENFree memory
$ free && sync && echo 3 > /proc/sys/vm/drop_caches && freeMerge config file
$ cat /<DIR>/* > /tmp/all.confFind out linux distribution name and vesion
$ cat /etc/*-releaseGenerate random string with length
$ openssl rand -hex <LENGTH>Retrieve filename from file/directory
$ for i in $(ls file/*.mp4); do echo $(basename $i .mp4); done
$ for i in $(cat filename); do echo $(basename $i .mp4); doneLast updated
Was this helpful?