Linux Command-line Basics#
Basics of Basics#
Navigate around the file system#
lslist filesls -llist files with detailsls -lalist all files with details including hidden filescdchange directorycd ..move out / up current directory by one layercd ~change to home Directorypwdprint current working directory
Create/Modify files and folders#
touch <filename>create a new filemkdir <dirname>make a new directory (folder)rm <filename(s)>remove file(s)rmdir <dirname(s)>remove empty directory(s)rm -rf <dirname(s)>remove directory(s) recursivelycpcopy files(s)mv <filename> <new filename>rename a filemv <filename(s)> <target path>move file(s) to target pathnano <filename>launch nano text editor to edit file content
Check system information#
lsb_release -acheck ubuntu versionechoprint functionsudo rebootrebootsudo shutdown -h nowshutdown nowifconfigiwconfigping <domain name or ip address>topshow processes; useqto quituname -acheck machine infolsusblist usblspcilist pci
User & Permissions#
User and groups#
Create a new user with home directory
sudo useradd -m username
List all users
less /etc/passwd
# or
users
List all groups
less /etc/group
# or
groups
List all groups that a specific user belongs to
groups USER
# or
id USER
List all users in a specific group
getent group GROUP
Add a user to a group
usermod -aG GROUP USER
- New group will be one of the user's secondary groups
Change a user's primary group
usermod -g GROUP USER
User password#
Change the current user's password
passwd
Change any user's password with sudo privileges
sudo passwd USER
Switch user#
sudo -ibecoming root user (require current user password)su -become root user (require root password)su [username]switch to user
File Permissions#
UNIX System Permissions
Example:
drwx------ 5 markhuang staff 160B Jan 13 11:13 .ssh
lrwxr-xr-x 1 markhuang staff 40B Jan 8 17:41 .tmux.conf -> ./dotfiles/tmux/tmux.conf
-rw------- 1 markhuang staff 797B Jan 9 16:39 .viminfo
- First char:
d/l/-stands fordir/link/file - following 3 chars:
r/-+w/-+x/-representRead,Write,Executepermission for owner user. - following 3 chars:
r/-+w/-+x/-representRead,Write,Executepermission for owner group. - following 3 chars:
r/-+w/-+x/-representRead,Write,Executepermission for other user.
Change owner for all files inside a folder
sudo chown USER[:GROUP] FILE(s)
# e.g. Change the owner and group of example.txt to user jack and group jack
sudo chown jack:jack example.txt
Change user permission of a file
chmod NEW_PERMISSIONS FILE
# e.g. Add execution permission for owner to example.sh
chmod u+x example.sh
# e.g. Add execution permission for owner and group to example.sh
chmod ug+x example.sh
# e.g. Remove execution permission for everyone to example.sh
chmod a-x example.sh
# e.g. Add read permission for everyone to example.sh
chmod a+r example.sh
# e.g. Change the permissions of example.sh to `rwxrwxrwx`
chmod 777 example.sh
u: permission for owner userg: permission for owner groupo: permission for other usera: permission for allugo: permission for all+: add permission-: remove permission777: permission equivalent torwxrwxrwx644: permission equivalent torw-r--r--
Disk#
Check disk utilization#
df -h
-hfor "Human-readable" output
Example:
df -x tmpfs -x squashfs -x devtmpfs -x vfat -hT
-xor--exclude-typefor excluding certain file systems Type-Tfor printing file system type
Check file/folder size#
du -sh {dir}
du -sh ./*
-sfor "specified" file/dir only-hfor "Human-readable" output
Working with Files and Folders#
mkdir new_folder_namemake directory (create a new folder)rmdir some_folderremove a empty folderrm -rf some_folderremove a folder and everything inside
More about removing directories: How to Remove (Delete) Directory in Linux
touch example.txtcreate a new filenano example.txtedit a file using the nano text editorvim example.txtedit a file using the vim text editorcat example.txtprint out file contentrm example.txtremove a file (delete a file)
move a file
mv [current path to file] [new path to file]
mv example.txt ~/Desktop/example.txt
rename a file
mv [current file name] [new file name]
mv example.txt example_renamed.txt
copy a file
cp [current path to file] [new path to file]
cp example.txt example_copy.txt
Copy files between machines#
scp also supports autocompletion for remote files and directories if you have existing SSH connection to the remote machine.
secure copy to remote machine
scp [local file] [remote user]@[remote address]:[remote path]
scp example.json root@178.128.22.33:/home/root/example
secure copy from remote machine
scp [remote user]@[remote address]:[remote file] [local path]
scp root@178.128.22.33:/home/root/example.json .
When copying a good number of files
- Consider using a simpler encryption algorithm
- Consider using
rsyncinstead ofscp - Consider using
parallel-scpandparallel-rsync