Hello, readers today we will learn about Linux and Linux commands.
Before getting started with Linux and important commands we must have a basic idea about the Operating system and all related stuff so in this blog we will start with an overview of OS and then some important Linux commands.
Operating system - what is it?
The operating system is a system software that acts as an interface between the user and the hardware of the system. It maintains hardware and provides a safe environment for running the program.
OS is loaded into memory when the computer is booted and remains as long as the system is on.
There are two important components of OS:- kernel and shell.
Kernel and the shell
The kernel is the core of the operating system, it contains a collection of routines mostly written in c. The kernel is loaded in memory when the system is booted and communicates directly with the hardware.
The shell is the command line interpreter, it translates commands into actions. when a user types a command the shell examines it for the special character when it finds the special character it rebuilds the command to a simpler version and communicates with hardware for command execution.
why linux
multi-user system
multiprogramming system
multi-tasking system
secure
robust
open source
Files and process
Unix system identifies everything as either files or process.
files may be collections of bytes, data, information etc. Unix system considers directories also as a file.
process is any file or executable file under execution.
Different types of file
Ordinary (regular ) file
Directory File.
Device File.
Ordinary file:- A directory file is the most common type of file, all programs we write belong to this type.
An ordinary file is divided into two types Binary file and Text File.
Directory File:- A directory file contains no data but keeps dtails of files and subdirectories it contains.
it contains the file and the unique identification number of each file and sub directory.
Device file:- Device files are generally found inside a single directory structure /dev.
It is a special type of file which in fact contains nothing.
image of the file structure in Unix
Path name - absolute and relative
An absolute path name is full path specifying the the location of a file or directory from the root directory or start of the actual file system.
absolute path name always start with root symbol that is "/"
Relative path name is the path with reference to our current working directory.
relative path name need not start with root.
File attributes
File attributes are important things to know because working with Linux systems we always have to deal with files.
when we execute the ls -l command in the directory then we get output with all file attributes using which we can play around with the file.
The file attributes are :
file type and permissions
Links
ownership
group ownership
file size
last modification time
file name.
Changing file permission
we can change file permissions in one of two ways
absolute
relative
in absolute mode we can explicitely change the permission without knowing the current permissions.
in relative mode we need to know current file permissions.
Now we have overview of what operating system is and basic understanding of linux systems.
Important Commands
pwd
will show the present working directory in which you're currently present.ls
will show you the list of all the files present in a specific directory.ls -a
will show you the list of all the hidden files present in a specific directory. Hidden files are starting from dot .ls -l
will show you the list of files with long details present in a specific directory.ls -la
will show you the list of more files including hidden files with long details present in a specific directory.ls -R
will find all the folders and sub folders and so on recursively.Dot . means current directory, .. double dot means previous directory.
cd
(change directory) will change the path location from one directory to another.cat filename
(concatenate) will print all the content of a file in a standard output.cat > filename
will create a new file if it is not present and allow us to enter the text also.tr
will translate the characters from one string to another string.cat lower.txt | tr a-z A-Z > upper.txt
, the output of the first command is the input of the second command.man command-name
will show you the details of a specific command.mkdir directory-name
will create a new directory.mkdir -p random/middle/hello
will create a middle directory b/w two directories.-p
is used for parent's command.touch filename
will create a new file.cp file.txt copy_file.txt
will make a copy of the file.txtcp -R test random
will copy the test directory into the random directory.mv file.txt random
will move the file.txt to the random folder.mv file.txt new_file.txt
will rename the file.txt to new_file.txt.mv test renamedTest
will rename the directory.rm file.txt
will remove a file from your computer permanently.rm -R directory-name
will remove the directory recursively.rm -rf directory-name
will forcefully remove the directory.sudo
(super user do) will be used when some commands require administrative permission. If you want to access some files that require admin permission then you will usesudo
to enter the password and give the permssion.df
is used to find the disk storage capacity.df -m
will show the data size in MBs.df -mh
will show the data in MBs and in human readable format.du
will estimate the disk space usage statistics.du -h
will give the file usage space in human-readable format.head
will print the first few lines of a file.head -n 4 file-name
will print the first four lines of a file.tail
will print the few lines from the bottom of a file.diff
will compare the content of the files line by line and see if there is any difference.where filename
will show you the list of directories in which a file is present.open .
will open all the files present in a specific directory.echo $PATH
When one types a command to run, the system looks for it in the directories specified by PATH. It will display the files and folders paths by the difference of colon : It will check the executable command in one of these paths.echo "Hey" > file.txt
will override the text in a file.echo "Hey" >> file.txt
will append the text in a file.export MY_PATH="shyam"
will create another path that will contain the string. But this is not permanentlocate
will find the files by name.find
search all the files present in a directory.find . -type d
will only find the directories that are present in a current directory.find . -type f
will only find the files that are present in a current directory.find . -type f -name "file-name"
will only find the files that are present in a current directory and the name of the file is given.-name
is case sensitive but if you don't want to use the case sensitive then use-iname
.mmin
(last modified n minutes ago)find . -type f -mmin -20
will show only the files that are modified in a current directory less than 20 minutes ago.find . -type f -mmin +15
will show only the files that are modified in a current directory more than 15 minutes ago.find . -type f -mmin +2 -mmin -10
will show only the files that are modified in a current directory more than 2 minutes ago and less than 10 minutes ago.find . -type f -mtime -10
will show only the files that are modified in a current directory less than 10 days ago.find . -type f -maxdepth 1
will only show the files that are present in the first directory. It is not going recursively. If you want to recursively search then make the maxdepth equal to 2, 3, and so on.find . -size +1M
will find all the files that has a size more than 1 MB.find . -empty
will find all the empty files.find . -perm 777
will find the files that has 7(read), 7(write), and 7(execute) permission.chmod u=rwx,g=rx,o=r file-name
If I write 777
, it will assign read, write, and execute permission to every hyphen of a file.
chmod 777 file-name
If I write 577
, it will assign read, and execute permission to the first three hyphens and read, write and execute permission to the remaining hyphens of a file.
chmod 577 file-name
How to perform a functionality on many files? Let's you want to find multiple files and delete them.
find . -type f -name "*.txt" -exec rm -rf {} +
will find and remove all the files.{}
will contain a list of all the files.whoami
will print the person username who is logged in.
grep(global regular expression print)
grep "text" filename
will find the text in a file and it is case-sensitive.grep -w "text" filename
will find the complete word written in a file.grep -i "text" filename
will find the text in a file even if it is not case-sensitive.grep -n "text" filename
will find the text with a line number in a file.grep -B 3 "text" filename
will find the lines that came before the given text.grep -rwin "text" .
will find the text recursively in a current directory.grep -wirl "text" .
will find all the files that contain the given text.grep -wirc "text" .
will count the files that contain the given text.history
will show the history of all the commands that has been used.history | grep "ls"
will show history of all the ls command that are used.grep -P "regular expression" filename
will find the specific text in a file by using regex.In Linux, an alias is a shortcut that references a command. Aliases are mostly used to replace long commands, improving efficiency and avoiding potential spelling errors.
alias
will show you all the aliases that are generated.alias <key>="<value>"
will make a new alias and after that, if you type a key, a functionality according to a value will be performed.unalias <key>
will remove an alias from the memory and after that, the key will not be functional.
Ctrl+A
will move you to the first point of the command.Ctrl+E
will move you to the end point of the command.Ctrl+U
will delete everything that you entered.Ctrl+K
will delete the command from the back of the cursor.Ctrl+R
will search for the previous command.TAB button
will auto complete the command without writing it as a whole.!<number>
will take a number from the history and run it to access that command.!<command>
will take a command from the history that was used last time and run it to access that command.;
will help you to add multiple commands in one line.sort file-name
will sort the data in an alphabetical order.sort -r file-name
is the reverse of the sort.sort -n file-name
will return the data in a numerical order.jobs
will display all the current processes that are running.ping website
will display all the data packets from a particular server.wget <URL>
will download files from the internet.wget -O file-name <URL>
will save the downloaded file under a different name.top
will show that how many processes are running and how much CPU usage is consuming?kill process ID
will close the process.uname
will display the kernel name.uname -o
will print the operating system.uname -m
will print the architecture.uname -v
will print the kernel version.cat /etc/os-release
will give the information of your operating system.lscpu
will give you the CPU details.free
will show you the free memory.vmstat
will display the virtual memory state.id
will print the IDs, group ids and stuff.getent group username
will look up the user details on Linux.zip
zip-file.zip
text-file.txt
will zip the text-file.txt into zip-file.txt.unzip
zip-file.zip
will unzip the file.hostname
will show the hostname.hostname -i
will show the ip-address.useradd username
will add a new user.passwd username
will give a password to the user.userdel username
will delete a username.lsof
will list all the open files.lsof -u username
will list the files that are opened by a username.nslookup website
will give the ip-address of a website.netstat
will give the details of all the active ports.ps aux
will give a snapshot of the current processes.cut -c 1-2 file-name
will remove sections from each line of files.ping website1 & ping website2
"&" operator will fetch multiple sites data.echo "first" && echo "second"
"&&" operator will say if the first command is executed then execute the second command.echo "hey" && {echo "hi"; echo "I am good"}
will say that if the first echo is executed then execute the commands in curly braces.echo "first" || echo "second"
"||" operator will say if the first command is not executed then execute the second one.| (pipe)
will send the output from the first command to the second command.