Getting Started With Linux
10 min read
Photo by Ian Parker on Unsplash
Table of contents
- Linux Directory Commands
- Linux File Commands
- Giving permission to file and directory
- Different ways of creating file in Linux:
- cat command
- echo command
- printf command
- vim command
- touch command
- Linux File Content Commands
- Linux User Commands
Linux Directory Commands
It is use to see the current working directory, starting from root(/). It will give you a path of in which directory you are standing right now.
It is use to see the list of all files and directories in current directory. ls command without any argument will enlist everythings of what directory has inside.
cd ec2-user command
It is use to go forward in the next directory from current directory in which you are now. You also can jump into subdirectories by using forward slash (/) then subdirectory name. For example: cd ec2-user/myfolder
cd .. command
It is use to come back in the previous directory from current directory in you are right now.
cd / command
It is use to come back in root directory from current directory. It doesn't matter in which subdirectory you are right now but this command pull you out from all.
cd ~ command
It is use to come back in home directory from current directory. It doesn't matter in which subdirectory you are right now but this command pull you out from all.
mkdir newfolder command
It is use to create a new directory in current directory you are right now. You can also create subfolders using this command by staying in parent directory. For example: mkdir newfolder/subfolder.
which newfolder command
It is use to check the complete path of any file, directory and any shell command. Mostly, we use this command when we want to know how much inside in subdirectories we are right now.
rmdir newfolder command
It is use to delete empty directory by name from current directory. It gives an error if directory is not found.
rm -r newfolder command
It is use to delete non-empty directory from current directory by name. It gives an error if directory is not found.
rm -r newfolder
mv newfolder myfolder command
It is use to rename a directory and a file. This will take existing file or directory name and new name from which you want to replace it.
mv newfolder myfolder
ls -l lib command
It is use to see the permission, size, time and date of lib directory. So, that we can edit permission, timestamp and name according to our need.
ls -l lib
ls -l command
It is use to enlist all the files and directories in which you are right now. Output will show permission of file, file size, timestamp and name about all files or directories.
Linux File Commands
Giving permission to file and directory
This is how we read the permissions of a directory and file:
Here d represents the directory, first rwx (read, write, execute) represents the Owner permission, second represents the Group permission and third represents the Others permission.
Here l represents the file, first rwx represents the Owner permission, second rwx (read, write, execute) represents the Group permission and third rwx (read, write, execute) represents the Others permission.
If any of these letters is replaced with a hyphen (-), it means that permission is not granted. For example:
A folder which has read, write and execute permissions for the owner, but only execute and read permissions for the group and execute permission for other users.
chmod drwxr-xr-x myfolder
A file that can be read and written by anyone, but not executed at all.
chmod lrw-rw-rw- myfolder
A file that can be read and written by the user, but only read by the group and everyone else.
chmod lrw-r--r-- myfolder
We can also represent this using numbers instead of letters. For example, 000 means "you are not giving permission for any modes to Owner, Group and Others" while 777 means "you are giving permission for all modes to Owner, Group and Others". Other number representations are:
N (R W X)
0 (No No No)
1 (No No Yes)
2 (No Yes No)
3 (No Yes Yes)
4 (Yes No No)
5 (Yes No Yes)
6 (Yes Yes No)
7 (Yes Yes Yes)
So, for example: 777 is the same as rwxrwxrwx, 555 is the same as r-xr-xr-x, 666 is the same as rw-rw-rw-, 444 is the same as r--r--r-- and 322 is the same as -wx-w--w-.
chmod 777 myfolder command
To give read, write and execute permission to your new directory.
chmod 777 myfolder
Use letter notation to assign permission:
You can use the letters (a) for all users, (u) for user/owner, (g) for group and (o) for others to set permissions for each of the user types, and r (read), w (write) and x (execute) to represent the permissions to set.
You assign permissions using either the plus sign (+), which means “add these permissions”, the minus sign (-), which means “remove these permissions”, or the equals sign (=), which means “change the permissions to exactly these”. For example:
chmod a+x ans.txt commandAdds execute permissions for all users to the file ans to makes the file executable.
chmod a+x ans.txt
chmod u=rwx ans.txt command
Sets read, write and execute permission just for the owner (the permissions for the group and for others remain unchanged).
chmod u=rwx ans.txt
chmod go-w ans.txt command
Removes write permission for the group and for others, leaving the permissions for the owner unchanged.
chmod go-w ans.txt
Different ways of creating file in Linux:
In Linux you can create files with different commands and syntax. Some are:
A most common command use to create one and multiple files in a Linux. Also, this command is use to create, update, concatenate, redirect, sort and display multiple files at once.
Create file command
cat > file2.txt
Edit file command
cat >> file2.txt
Save/Exit file command
Delete file command
rm -r file2.txt
It is use to print string which given as argument, also print the multiline of text and to create and save it in a new file.
create file command
echo "hi, how are you?" > file4.txt
It works as printf command works in C language. This is use to print the string, integer arguments values and any other format specifier on the terminal window. Also use to save that values in a new file.
create file command
printf "hi, how are you?" > file4.txt
Further you can edit and execute these files by using same cat multiple commands.
vim is a text editor also vim is use to create text editor file to play with any type of text. We mostly used it to edit and create computer programs.
create file command
After open file Press (i) to insert new text. To save the file and leave vim, press Esc + : + x. And reopen file write vim file6.txt and also can use cat commands.
To change the date and time of a file and directory. Also, we can assign date and time according to our choice.
command to assign current time and date
touch -m file2.txt
Linux File Content Commands
This command is use to check the last 10 lines data of file6.txt. Showing last ten lines is default. Mostly, we use this command when we want to confirm new inserting data.
Here we can also fetch the number of lines according to our need. This command is use to check the last 5 lines data of file6.txt.
tail -n 5 file6.txt
It is use to check the first 10 lines data of file6.txt. Showing first ten lines is default. This command is opposite of tail command.
Here we can fetch the number of lines according to our need. This command is use to check the first 5 lines data of file6.txt.
head -n 5 file6.txt
It is use when we want to extract some data by matching a regular expression. This search for the string and extracts complete lines which contain that string.
grep "brother" file8.txt
It is use to count how much time a regular expression has repeat in a mentioned file.
grep -c "brother" file8.txt
It is use to get the details of a mentioned file. This count the total lines, total words and byte size of a file.
It is use to copy file from source to destination. And syntax be like: cp source destination
cd file5.txt /home/ec2-user/myfolder
It is use to move a file from source to destination. And syntax be like: mv source destination
mv file5.txt /home/ec2-user/file5.txt
We also can change the file name in moving path of file.
mv file5.txt /home/ec2-user/file7.txt
Linux User Commands
It is use to end the shell session and exit the script from where it is currently running.
Sudo stands for super user and is use to access restricted operations and files. It allows ordinary users to perform an administrative task. It prompts you for your personal password and confirms your request to execute a command by checking a file, called sudoers , which the system administrator configures.
It is use with sudo in Debian based Linux distributions to install specific packages.
It is use to check the username whom is currently logged in.
echo "Hey $USER"
It is use to change the current password. It will ask your old password and then new password and will set it automatically. Sometimes the root user reserves the privilege to change the password for any user on the system but a normal user can change the password for own account.
It is use to check the current shell session running processes. It will show process ID, TTY, time and command name.
It is use to view information about system like kernel version, shell, and hardware.
It is use to kill some processes which is unresponsive and showing inappropriate behavior. You can either use process id or program's binary name with kill command.
It is use to test the network connectivity. Also, it helps different connectivity issues with troubleshooting.Here it will try to connect with google.
It is use to retrieve content from the internet in your system. This command will download the mentioned user yolo5 Github repository in your system.
shut downnow command
It is use to shut down the computer right now.
shutdown 15:00 command
It is use to shutdown the computer at 15 o'clock.
shutdown -c command
It is use to cancel last set shut down call.
It is use to reboot computer right now.
shutdown -r +5 command
It is use to reboot computer within 5 minutes.
shutdown -r +5