clear
sudo apt=get update && sudo apt-get upgrade
whoami
hostname
ls
ls -l
To list hidden folder and files
ls -la
Folders
ls -l -G
ls -l --no-group
man ls
Print Working Directory
pwd
**Print Environment Variable
printenv
change directory
cd /bin
Home directory
cd ~
Entries that starts with p
ls -l /bin/p*
At root
ls -l /
ls -l /var
Files and User Permissions
Below is the result of ls -l
command:
-rwxr-xr-x 1 vinkrish staff 3818 Feb 1 2000 program.sh
Observations:
-
beginning character tells it's a file, d is for directory- Next 3 characters specify read, write and execute permissions for owner
- Next 3 characters specify group permissions
- Next 3 characters specify other user permissions
- 1 specifies file references
- vinkrish: Owner
- staff: Group
- 3818: file size in bytes
- 02/01/2000: Last modified date
- program.sh: name of the file
Switch user to root user for the session, terminal will show now running as root (avoids entering sudo each time!)
sudo su
Environment Variable
export PORT=3000
process.env.PORT
To print out content of file
cat /var/file.txt
To print line numbers
cat -n /var/file.txt
tail /var/file.txt
To open just content without previous commands
less /var/file.txt
g -> go to beginning of file
Shift + g -> go to end of file
/word + enter -> searches the word in file
:q -> quit
Open folder or file in default app
open .
open folder/file.txt
open folder/file.asd -a TextEdit
To create a file
touch file.txt
Write to a file (overwrites)
echo 'hello world!' > file.txt
To append file
echo ' appended' >> file.txt
To create folder + intermediary folders with -p flag
mkdir folder_name
mkdir -p outer_folder/middle_folder/inner_folder
ls outer_folder/middle_folder
outputs inner_folder
Delete file and folder
rm file.txt
rm -r folder_name
rm -rf folder/
rm -r *
Move Files
mv source destination
mv file.txt folder_name/file.txt
Rename file
mv file.txt renamed_file.txt
Rename folder
mv folder_name/ renamed_folder_name
Move all content from one folder to another
mv source_folder/* destination_folder/
Copy file
cp file.txt destination_folder/file.txt
Copy all files
cp -R source_folder/* destination_folder/
Symbolic Link
ln -s existing_file symbolic_link_file
ln -l symbolic_link_file
rm symbolic_link_file
unlink symbolic_link_file
ln -sf existing_file overwrite_symbolic_link_file
ln -s /Users/vinkrish/Workspace ~/workspace
man ln
Extract tgz file
tar -xvf file.tgz
tar -xvzf file.tar.gz
tar -xvf file.tar.gz
Find png images in a folder
find image_folder/ -name "*.png"
Case insensitive search
find image_folder/ -iname "*.jpg"
All folders (outer + inner)
find . -type d
Find folders having images in their name
find . -type d -name "images"
Deleting with find
find dist/ -name "*.built.js" -delete
Add execute permission to user
chmod u+x script.sh
Add Write persmission to all users
chmod a+w /var/file.txt
Remove Write persmission to all users
chmod a-w /var/file.txt
Change owner
sudo chown vinay /var/file.txt
Change permission so that you don't have to use sudo
chmod u+w /var/file.txt
find /var/log/syslog* -print0 | xargs
find /var/log/syslog* -type f
To use pipes properly
find /var/log/syslog* -type f -print0 | xargs -0 chmod a+r
To search in concatinated file content
grep looks for matching lines in command input
find /var/log/syslog* -type f -print0 | xargs -0 cat | grep "SearchTerm"
find /var/log/syslog* -type f -print0 | xargs -0 cat | grep "SearchTerm.*filterFurther"
To count number of occurances:
find /var/log/syslog* -type f -print0 | xargs -0 cat | grep -c "SearchTerm.*filterFurther"
ifconfig
ifconfig | grep -E "inet6.*autoconf"
ifconfig | grep -E "inet6.*autoconf" | head -n 1
ifconfig | grep -E "inet6.*autoconf" | head -n 1 | cut -d " " -f 2
Text editor
nano file-name.sh
To generate public and private key with comment to identify
ssh-keygen -b 2048 -t rsa -C vinay@localhost.com
(or)
ssh-keygen -t ed25519 -C "<comment>"
cd ~/.ssh
look at the permission
ls -l
ls .ssh
cat .ssh/id_rsa.pub
Save the public key on server
ssh-copy-id -i .ssh/id_rsa.pub vinay@machine-name
Copy file from a remote host to local host
scp username@from_host:file.txt /local/directory/
Copy file from local host to a remote host
scp file.txt username@to_host:/remote/directory/
Copy directory from a remote host to local host
scp -r username@from_host:/remote/directory/ /local/directory/
Copy directory from local host to a remote host
scp -r /local/directory/ username@to_host:/remote/directory/
Copy file from remote host to remote host
scp username@from_host:/remote/directory/file.txt username@to_host:/remote/directory/
#!/bin/bash
while [ : ]
do
originalAddress=$(ifconfig | grep -E "inet6.*autoconf" | head -n 1 | cut -d " " -f 2)
echo "$(date) $originalAddress" >> ~/ip.txt
sleep 10
done
- Specify BASH shell
- assign value to variable (note: no spaces around
=
) - make script executable
- run script
- watch file for appended content with flag
-f
chomod u+x script.sh
./script.sh
tail -f ip.txt
#!/bin/bash
originalAddress=$(ifconfig | grep -E "inet6.*autoconf" | head -n 1 | cut -d " " -f 2)
echo "$(date) $originalAddress" >> ~/ip.txt
crontab -e
* * * * * ~/script.sh
crontab -r