UNIX-1.8 ps, netstat, kill, pidof, fuser, awk commands


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

ps -fu username – displays all processes started by user
ps -ef – display all processes running
ps -ef | grep ‘pname’ – look for specific process
netstat -t – see all tcp connections
netstat -an – see all network connections
netstat -c – netstat will execute every second
netstat -p – display processes that use these connections
kill pid – kills process by pid
kill -9 pid – force kill process by pid
& – execute process in background
free – show memory usage, alternatively use top
pidof command – shows process id for running process/command
fuser -vu dir – displays all processes that are using dir, along with owner id of processes
echo “hello world” | awk ‘{ print length($1) }’ – prints length of field
echo “hello world” | awk ‘{ print substr($1, 3) }’ – prints llo, substring function
echo “hello world” | awk ‘{sub(regex, replacement); print}’ – replace pattern on line and print
echo “HELLO world” | awk ‘{ print tolower($1) }’ – lower case field 1
echo “hello world” | awk ‘{ print toupper($1) }’ – upper case field 1

Advertisements

UNIX-1.7 Find Command


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

find . -iname ‘regex’ – find filenames that match regex, case insensitive
find . -mtime +2 – find files that were modified 2 days ago or older
find . -mtime -2 – find files that were modified within past 2 days
find . -ctime +2 – find files that were created 2 days or older
find . -atime +2 – find files that were accessed 2 days or older
find . -cmin +30 – find files that were created 30 minutes ago or older
find . -mmin +30 – find files that were modified 30 minutes ago or older
find . -empty – find empty files or directory
find . -executable – find files that are executable by user
find . -readable – find files that are readable by user
find . -writable – find files that are writable by user
find . -size filesize – find files that have specified filesize (c = byte, k = kb, M = Mb, G = Gb)
find . -type filetype – find by file type (d = directory, f = file, l = link)
find . -user username – find files owned by user
find . -group groupname – find files that belong to specific group
find . -name ‘regex’ -delete – remove files that match regex
find . -name ‘regex’ -exec command – execute command on files found
find . -name ‘regex’ -ok command – same as -exec, but prompts for user’s input

UNIX-1.6 grep, useradd, usermod commands


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

last -10 username – get last 10 logins of specific user
passwd – change password
diff -y file1 file2 – find differences between 2 files
grep ‘regex’ file – search for pattern
grep -i ‘regex’ file – case insensitive search
grep -P ‘regex’ file – full support of Perl regex
grep -e ‘regex1’ -e ‘regex2’ file – search for multiple patterns
grep -v ‘regex’ file – inverse of search pattern (return non-matching lines)
grep -n ‘regex’ file – display line number with search result
grep -B4 ‘regex’ file – also display 4 lines before match
watch -n5 command – executes command every 5 second
adduser username – add a new user
usermod username -d /home/newhome – change home for new user
usermod username -d /home/newhome -m – change and move home content to a new home
usermod username -e 2022-05-11 – set account expiration date
usermod username -g group_name – set user group name/id
usermod username -l new_username – change username for user
usermod username -p new_password – change password for user
usermod username -s shell_name – change default shell for user
sudo -u username -i – run future commands as another user

Regex-1.1 Regular Expression Introduction


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

\w – matches a alphanumeric character
\d – matches a number
\s – matches a space
\W, \D, \S – opposite of \w, \d, \s
^ – beginning of line
$ – end of line character
. – match any character except for newline
* – 0 or more occurrence
+ – 1 or more occurrence
? – 0 or 1 occurrence
| – alternative match
(regex) – grouping/store match
[ ] – character set/class
{ x,y } – number of occurrence between x and y
{ x } – x occurrence
{ x, } – at least x occurrence
[^ ] – opposite of [ ]
*? – non greedy matching