DEV Community

Back2Basics
Back2Basics

Posted on

Linux Fundamentals

Linux is open source operating system widely used to run large workloads. To be a professional Software engineer Linux is very basic skill required. Here in this blog I will give some foundation knowledge on Linux commands and its usage. Most of linux administrative tasks rely on working with filesystems, running applications, troubleshooting the workloads, monitoring the activities. It also involves lot of repetitive work for this we have shell scripting to automate the linux administrative task. Lets focus on Linux basic commands.

1. Working with Directories:

Create directory for maintaining some files.

# mkdir <directory-name>
mkdir Frontend
Enter fullscreen mode Exit fullscreen mode

Create multiple sub-directories in the current directory

# mkdir -p <directory-name>/<sub-directory-1>/<sub-directory-1>
# the numbers are sub-directories.
mkdir -p Frontend/1/2/3/4/5
Enter fullscreen mode Exit fullscreen mode

2. Creating files:

create an empty file.

# touch <file-name>
touch testfile
Enter fullscreen mode Exit fullscreen mode

Create file and store the content immediately

echo "Hello Linux" > testfile
Enter fullscreen mode Exit fullscreen mode

Add some more test into file via commandline

echo "Commands are essential" >> testfile
Enter fullscreen mode Exit fullscreen mode

Using Heredocs

cat <<EOF > testfile
Hello World
Linux is Open source Operating system
Lets learn the basic commands
EOF
Enter fullscreen mode Exit fullscreen mode

create a file in the directory with absolute path

touch /<home-directory>/<user-name>/<directory-name>/testfile
Enter fullscreen mode Exit fullscreen mode

3. Copy files/Directories

copy file content from one file to other file

cp [source-file-name] [destination-file-name]
Enter fullscreen mode Exit fullscreen mode

copy file content to other directory file(if this file exists then just copies else creates the destination file and copies the content)

cp [source-file-name] <some-directory-path>/[destination-file-name]
Enter fullscreen mode Exit fullscreen mode

copy the files from one location to other location. The option **"-r" **for recursive. If the source directory exists then in the destination the source file content only copies otherwise in the destination directory the source directory will be created then copied the content(files/directories).
Note: the "/" end of directory is always suggested to use not mandatory.

cp -r <source-directory-path>/ <destination-directory-path>/
Enter fullscreen mode Exit fullscreen mode

4. Moving files/Directories:

rename the filename

# mv [source-file-name] [dest-file-name]
mv testfile-old test-file-new
Enter fullscreen mode Exit fullscreen mode

rename the directory name. We can also pass the absolute paths or relative paths.

Note:
- Absolute Path: the path from the root directory to end directory/file we need
- Relative Path: The path relative from the current directory we want to reach the other directory.The path with ../Drawing-dir one step up from current directory or ~/Desktop is also relative directory for Desktop directory. Its absolute path is /home/bob/Desktop. Here bob is the username.

# mv [source-directory-name]/ [destination-directory-name]/
mv Frontend/ frontendDev/
Enter fullscreen mode Exit fullscreen mode

move the content from the current directory to other directory

# mv [source-directory] [destination-directory]
mv /home/bob/Frontend /home/bob/Desktop/Frontend
Enter fullscreen mode Exit fullscreen mode

5. Removing files/Directories:

Remove a file

# rm -f [file-name]
rm filename
# rm -f <path-to-file-name>
rm  /home/bob/testfile
Enter fullscreen mode Exit fullscreen mode

Remove directory. Here "-r" for recursive to forcefully delete "-f" is the option.

# rm -r [directory-name]
rm -r /home/bob/frontend/
Enter fullscreen mode Exit fullscreen mode

6. Hard Links and Soft Links:

Lets say there were two users bob and steve. The user bob created one file and copied the file to steve home directory. Now the both files are in the same host machine but in different locations. Now bob deleted the file created still steve has the file. What is the reason behind it? every file we create has its own inode which is the address of the file pointer it stored data in the filesystem memory. So after copying the file from bob to steve home directory. The file has two hard links created by filesystem.You can check by running the command.

# stat <path-to-filename>
stat /home/bob/testfile
Enter fullscreen mode Exit fullscreen mode

we can also create hardlinks by the command

ln  testfile testlink
Enter fullscreen mode Exit fullscreen mode

now check the by stat command. The testlink is still exists in the same directory it created. The ls command shows the long listing all files/directory information.

ls -la 
Enter fullscreen mode Exit fullscreen mode

If you rename/remove the source file then the hard link will be broken.
To work with soft links

ln -s <path-to-source-file/Directory> <symlink-name>
Enter fullscreen mode Exit fullscreen mode

check the no of soft links by stat command.

7. listing files/Directories:

List the files by ls command. In the current directory

ls
Enter fullscreen mode Exit fullscreen mode

list items in the /home/bob/Desktop

ls /home/bob/Desktop
Enter fullscreen mode Exit fullscreen mode

Pass the options for more details
Options:
-l for long listing view of items in the directory
-a for all files/directories including the "dot" file/hidden files
-t for time of items creation
--full-time to see the last modified time.
For more options take the ls --help help.

8. Ownership and Groups:

change the group ownership of file/directory

# sudo chgrp <group-name> <file/directory>
chgrp sudo <file-name> # adding file to sudo group
Enter fullscreen mode Exit fullscreen mode

To list groups

groups
Enter fullscreen mode Exit fullscreen mode

Change ownership of file/directory

# sudo chown <user-name>:<group-name> <file-name>
sudo chown bob:sudo /home/bob/testfile
Enter fullscreen mode Exit fullscreen mode

The General File Types and their Identifiers

Image description

run the ls -l command it will give the details in the first column the permissions allowed to the files/directories.

Image description

In Octal representation

Image description

Top comments (0)