Linux commands for beginners
This blog lists all the basic commands to get started with Linux. Happy learning!
Table of contents
Hello everyone, let's get started
Basic commands
ls - List files
cd - change directory
cat - To view the contents of file
echo - print
rm - remove files directories
mv - move
cp - copy
Here we can see five files in the A-folder directory. The cp (copy) command created a new file, hello.sh, and kept the old file as it is. Also, the content in both files will be the same. Whereas for mv (move) command, the content will be the same but the file creation time will be that of the old file, and the old file name won't be visible.
Date time of creation in case of cp command - as and when the command is executed
Date and time of creation in the case of mv command: when the old file was created. i.e cp oldfile newfile : Oldfile contents will be copied to new file, old file and new file both will be in the directory. The new file date of creation will be when the command is executed.
mv oldfile newfile: Old file will not be there in the dir, contents would be copied to new file and date of creation would be the date of old file creation. In simple terms it is just renaming the file.
grep - find text wherever you want.
case sensitive command
for ignoring cases - grep -i
Syntax: grep word_name file_name
This finds the word in the given file.
pipe ( | ) -
redirect (>)
find
ls -l
sudo - Gives you full permission of everything, now you can execute anything. You become the root user
chown - Change owner
chmod - Change modes read write execute
User (u): A user is the one who created the file. By default, whoever creates the file becomes the owner of the file. A user can create, delete, or modify the file.
Group (g): A group can contain multiple users. All the users belonging to a group have the same access permission for a file.
Other (o): Anyone who has access to the file other than the user and group comes in the category of other. Other has neither created the file nor is a group member.
Other symbols: "+" Add to existing , "-" Remove , "=" Assign
Simplest way to remember is :
Read - 4, Write - 2, Execute - 1
if you want to give rwx access, add the numbers 4+2+1= 7
chmod 700 file_name -- this gives rwx permissions to users of the file, no permissions for group and owner.
Change ownership to folders
chown -R folder_name owner_name : this command changes owner of all folders inside folder_name, including folder_name owner change.
chgrp -R grp_name folder_name: change grp pf all folders recursively
history command - Gives all the commands you have executed.
Variable : $var
$ - dollar sign is known as an expansion operator, it expands the value of the variable.
myvar = 573
echo "$myvar" //prints 573
echo "Variable value is $myvar"
unset - unset var_name
You can use variables as commands
here we have assigned "ls" as a value to variable myvar
Now when we echo it just prints ls, whereas when we only use the variable we get the list of files in the current directory.
More examples:
Global variables : $USER, $HOME, $PATH
PATH - list of executables, everything that we type all commands are contained inside this path.
Command substitution :
Here we have substituted value "ls" in variable "list", when we do $list, all files are listed.
User profile: .profile .bashrc .bash_profile .login --> Check for these files any 1-2 files might be there on your system.
To find these files execute the following command: ls -a
Let's see the content of .profile
Here I have added a line Hello Ubuntu User, now whenever I open Ubuntu I get this line printed. You can save variables in this file so that anytime you open Ubuntu you can access these variables.
read: Extremely useful for taking user inputs in scripting
read -p :
In this option, you can add prompts so that the user can see what to enter.
read -s: You can type without anyone knowing like for typing passwords
Using both options together:
read -sp:
These were all the basic commands to get you started with Linux, In the next blog I will talk about Shell scripts.
set -x : Debug mode, gives the command and the output
set -e: exits the script when there is an error, does not catch pipe failures.
set -o pipefail: stops in case of pipe failure
ps - active processes
nproc - no of CPUs
free - free memory on disk
df - disk space
top - node status
awk command
Thank You!