Enjoy your Linux journey! ๐๐งโจ
Linux Zero to Hero Commands!
Don't be intimidated by the terminal; mastering Linux commands can be an enjoyable and empowering experience! ๐ค
To view what's written in a file.
cat file.txt
To change the access permissions of files.
chmod 777 file.txt #owner-group-other has read-write-execute permission
To check which commands you have run till now.
history
To remove a directory/ Folder.
rm -rf file.txt rmdir -rf /folder/myfolder
To create a fruits.txt file and to view the content.
vim fruits.txt #create a file
Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
# After creating file using vim, type 'i' to insert content as - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava #save it by ':wq!' cat fruits.txt #see the content of the file
To Show only top three fruits from the file.
head -3 fruits.txt
To Show only bottom three fruits from the file.
tail -3 fruits.txt
To create another file Colors.txt and to view the content.
touch colours.txt cat colours.txt
Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.
vim colours.txt # Red, Pink, White, Black, Blue, Orange, Purple, Grey. #OR echo "Red" >> Colors.txt echo "Pink" >> Colors.txt
To find the difference between fruits.txt and Colors.txt file.
diff <fruits.txt> <Colors.txt>
Write a Shell Script to take user input, input from arguments and print the variables.
########### WRITE A SIMPLE CODE TO TAKE INPUT FROM THE USER & PRINT ########## #!/bin/bash echo "Hello, who am I talking to?" # Ask the user for their name read varname echo "It is nice to meet you $varname"
Write an Example of If else in Shell Scripting by comparing 2 numbers
#!/bin/bash echo "Enter a number:" read num if [ $num -gt 0 ]; then echo "It's a positive number!" elif [ $num -lt 0 ]; then echo "It's a negative number!" else echo "It's zero!" fi
Create 90 directories (day1 .. day 90)within seconds using a simple command
mkdir day{1..90}
Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is directory name and second is start number of directories and third is the end number of directories ) it creates specified number of directories with a dynamic directory name.
# $1-Day(Directory Name) $2-StartDay $3-EndDay #!/bin/bash start_day=$2 end_day=$3 for (i=start_day;i<=end_day;i++) do mkdir $1$i done
Execute as ./createDirectories.sh day 1 90
if we run like
./
createDirectories.sh
Movie 20 50
then it creates 50 directories asMovie20 Movie21 Movie23 ...Movie50
Create a Script to backup all your work done till now. backup.sh
#!/bin/bash src_dir=/home/ubuntu/myprofile/scripts/*.sh target_dir=/home/ubuntu/backup current_time=$(date +"%Y-%m-%d-%H:%M") backup_file=$target_dir/$current_time.tgz echo "Started Backup for $src_dir" tar -czf $backup_file --absolute-names $src_dir echo "Backup Completed!"
Automate the backup Script - Visit https://crontab.guru/
* 2 0 * * /root/backup.sh #first of every month at 2 am.
Create 2 users and just display their Usernames
sudo useradd user1 sudo useradd user2 cat /etc/passwd
Create a simple file and see the details of the files
touch file1.txt ls #see the files ls -a # -a show hidden files as well
Change the owner of a file from ubuntu to kshitija
sudo chown kshitija file.txt #Now owner of file.txt is kshitija ls -ltr
Modify file permission
sudo chmod 755 file.txt ls -ltr
ACL commands
getfacl
andsetfacl
getfacl <file or directory name> #To add permission for user -m : modify setfacl -m u:kshitija:rwx test/declarations.h #To add permissions for a group setfacl -m "g:group:permissions" /path/to/file #To remove ACL permission of user: -x = remove setfacl -x "u:user:permissions" /path/to/file #To remove ACL permission of group: setfacl -x "g:group:permissions" /path/to/file
You have to install docker and jenkins in your system from your terminal using package managers
#!/bin/bash #Docker Installetion sudo apt update sudo apt install docker.io -y sudo apt install docker-compose -y #Jenkins Installetion #for jenkins firstly you need java so sudo apt update sudo apt install openjdk-11-jre -y #Jenkins curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \ /usr/share/keyrings/jenkins-keyring.asc > /dev/null echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \ https://pkg.jenkins.io/debian-stable binary/ | sudo tee \ /etc/apt/sources.list.d/jenkins.list > /dev/null sudo apt-get update sudo apt-get install jenkins -y #for start jenkins sudo systemctl start jenkins sudo systemctl enable jenkins
๐ File and Directory Management
ls
: List files and directories in the current location.cd
: Change the current directory.pwd
: Print the working directory โ know where you are!mkdir
: Create a new directory.touch
: Create a new file.cp
: Copy files or directories.mv
: Move or rename files or directories.rm
: Remove files or directories (be cautious with this one! ๐ ).
๐ Networking Magic
ping
: Check network connectivity to a specific server or IP address.ifconfig
: Display network interface configuration (older command; useip
command on newer systems).ssh
: Securely connect to a remote server.wget
: Download files from the web directly in the terminal.
๐ง System Configuration
sudo
: Execute a command with superuser (root) privileges.top
: Monitor system processes and resource usage.apt
: Advanced Package Tool - install, update, or remove software packages (Debian-based systems).yum
: Install, update, or remove software packages (Red Hat-based systems).
๐ก๏ธ Security
passwd
: Change your user password.chmod
: Change file permissions.chown
: Change file ownership.
๐ Date and Time
date
: Display or set the system date and time.
๐ฎ Trash Talk
rm
(with caution!): Move files to the trash instead of permanently deleting them. Usealias
to set up a trash command.
๐ Bonus Tips
Use
man
followed by a command to access the manual page and learn more about it (e.g.,man ls
).Combine commands using pipes (|) to create powerful one-liners.
THANK YOU! :)