Linux Notes
Linux Notes
# MAN Pages
man command # => Ex: man ls
# opening a terminal
CTRL + ALT + T
--------------------------------------------------------------------------------------------------------------------------
## Bash History
# running a specific command from the history (ex: the 20th command)
!20
# running a command as root (only users that belong to sudo group [Ubuntu] or wheel
[CentOS])
sudo command
# installing tree
sudo apt install tree
# displaying mtime
ls -l
ls -lt
# displaying ctime
ls -lc
# creating an empty file if it does not exist, update the timestamps if the file exists
touch file.txt
# concatenating 2 files
cat filename1 filename2 > filename3
# less shortcuts:
# h => getting help
# q => quit
# enter => show next line
# space => show next screen
# /string => search forward for a string
# ?string => search backwards for a string
# n / N => next/previous appearance
# creating a new file or updating the timestamps if the file already exists
touch filename
# being verbose
cp -v file1 file2
-------------------------------------------------------------------------------------------------------------------------
# moving only if the source file is newer than the destination file or when the destination file is
missing
mv -u file1 dir1/
--------------------------------------------------------------------------------------------------------------------------
### The rm command ###
# removing a file
rm file1
# removing a directory
rm -r dir1/
## Piping Examples:
## Command Redirection
# output redirection
ps aux > running_processes.txt
who -H > loggedin_users.txt
# appending to a file
id >> loggedin_users.txt
## LOCATE ##
# updating the locate db
sudo updatedb
# displaying statistics
locate -S
## FIND ##
find PATH OPTIONS
# Example: find ~ -type f -size +1M # => finding all files in ~ bigger than 1 MB
## Options:
# -type f, d, l, s, p
# -name filename
# -iname filename # => case-insensitive
# -size n, +n, -n
# -perm permissions
# -links n, +n, -n
# -atime n, -mtime n, ctime n
# -user owner
# -group group_owner
--------------------------------------------------------------------------------------------------------------------------
## Searching for text patterns (grep)
Options:
-n # => print line number
-i # => case insensitive
-v # inverse the match
-w # search for whole words
-a # search in binary files
-R # search in directory recursively
-c # display only the no. of matches
-C n # display a context (n lines before and after the match)
--------------------------------------------------------------------------------------------------------------------------
## VIM
## IMPORTANT FILES
# /etc/passwd # => users and info: username:x:uid:gid:comment:home_directory:login_shell
# /etc/shadow # => users' passwords
# /etc/group # => groups
Exemple:
useradd -m -d /home/john -c "C++ Developer" -s /bin/bash -G sudo,adm,mail john
# creating a group
groupadd group_name
# deleting a group
groupdel group_name
## Monitoring Users ##
who -H # => displays logged in users
id # => displays the current user and its groups
whoami # => displays EUID
## LEGEND
u = User
g = Group
o = Others/World
a = all
r = Read
w = write
x = execute
- = no access
stat /etc/shadow
File: /etc/shadow
Size: 1721 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 524451 Links: 1
Access: (0640/-rw-r-----) Uid: ( 0/ root) Gid: ( 42/ shadow)
Access: 2020-08-24 11:31:49.506277118 +0300
Modify: 2020-08-22 14:43:36.326651384 +0300
Change: 2020-08-22 14:43:36.342652202 +0300
Birth: -
stat /usr/bin/umount
File: /usr/bin/umount
Size: 39144 Blocks: 80 IO Block: 4096 regular file
Device: 805h/2053d Inode: 918756 Links: 1
Access: (4755/-rwsr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-08-22 14:35:46.763999798 +0300
Modify: 2020-04-02 18:29:40.000000000 +0300
Change: 2020-06-30 18:27:32.851134521 +0300
Birth: -
# setting SUID
chmod u+s executable_file
chmod 4XXX executable_file # => Ex: chmod 4755 script.sh
stat projects/
File: projects/
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 805h/2053d Inode: 266193 Links: 2
Access: (2750/drwxr-s---) Uid: ( 1001/ student) Gid: ( 1002/ student)
Access: 2020-08-25 11:02:15.013355559 +0300
Modify: 2020-08-25 11:02:15.013355559 +0300
Change: 2020-08-25 11:02:19.157290764 +0300
Birth: -
# setting SGID
chmod 2750 projects/
chmod g+s projects/
stat /tmp/
File: /tmp/
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 805h/2053d Inode: 786434 Links: 20
Access: (1777/drwxrwxrwt) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-08-22 14:46:03.259455125 +0300
Modify: 2020-08-25 10:49:53.756211470 +0300
Change: 2020-08-25 10:49:53.756211470 +0300
Birth: -
## UMASK
# displaying the UMASK
umask
# starting top
top
## top shortcuts while it's running
h # => getting help
space # => manual refresh
d # => setting the refresh delay in seconds
q # => quitting top
u # => display processes of a user
m # => changing the display for the memory
1 # => individual statistics for each CPU
x/y # => highlighting the running process and the sorting column
b # => toggle between bold and text highlighting
< # => move the sorting column to the left
> # => move the sorting column to the right
F # => entering the Field Management screen
W # => saving top settings