Linux Command-line Basics#
Basics of Basics#
Navigate around the file system#
ls
list filesls -l
list files with detailsls -la
list all files with details including hidden filescd
change directorycd ..
move out / up current directory by one layercd ~
change to home Directorypwd
print 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) recursivelycp
copy 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 -a
check ubuntu versionecho
print functionsudo reboot
rebootsudo shutdown -h now
shutdown nowifconfig
iwconfig
ping <domain name or ip address>
top
show processes; useq
to quituname -a
check machine infolsusb
list usblspci
list 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 -i
becoming 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
,Execute
permission for owner user. - following 3 chars:
r
/-
+w
/-
+x
/-
representRead
,Write
,Execute
permission for owner group. - following 3 chars:
r
/-
+w
/-
+x
/-
representRead
,Write
,Execute
permission 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 torwxrwxrwx
644
: permission equivalent torw-r--r--
Disk#
Check disk utilization#
df -h
-h
for "Human-readable" output
Example:
df -x tmpfs -x squashfs -x devtmpfs -x vfat -hT
-x
or--exclude-type
for excluding certain file systems Type-T
for printing file system type
Check file/folder size#
du -sh {dir}
du -sh ./*
-s
for "specified" file/dir only-h
for "Human-readable" output
Working with Files and Folders#
mkdir new_folder_name
make directory (create a new folder)rmdir some_folder
remove a empty folderrm -rf some_folder
remove a folder and everything inside
More about removing directories: How to Remove (Delete) Directory in Linux
touch example.txt
create a new filenano example.txt
edit a file using the nano text editorvim example.txt
edit a file using the vim text editorcat example.txt
print out file contentrm example.txt
remove 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
rsync
instead ofscp
- Consider using
parallel-scp
andparallel-rsync