FileSyetmin Linux
FileSyetmin Linux
1. ls
The ls command is used in Linux/Unix systems to list directory contents. It's a fundamental command
and has various options to display information in different formats. Here are some of the common
options used with the ls command:
Basic Usage:
ls: Simply lists the names of files and directories in the current working directory.
Options:
Long Format:
• -l: Displays detailed information about files and directories in long format, showing
permissions, owner, group, size, modification time, and name.
• -a: Shows all files, including hidden files (those starting with a dot .).
• -h: Prints file sizes in a human-readable format (e.g., KB, MB, GB).
Sorting:
Directories:
• Ext3
• Ext4
• XFS
• FAT
Commands:
$: represents Normal user
• To find a folder if multiple folders are there: find folder1/ -name folder2
• Now if there are directories with different names present in folder1
• Creating a file: touch file1
• Now wants to find this file then,
Practice commands
Switch to root user and where as an Other user we created the file there create the another file but
now as an root user
Now exit from root user and as an other user try to delete that file2 created by Root user.It will show
permission denied.
To see the file content we can use cat command or less command
Ls
• c: Create a new archive. This flag tells tar to create an archive file.
• v: Verbose mode. This flag provides detailed information about the files being processed,
displaying the names of the files as they are added to the archive.
• f: File. This flag specifies the filename of the archive. The f flag should be followed by the
name of the archive file.
Now compress this file
Gzip files.tar
Now if we want to move this zip file from folder1 to folder5 which is present inside folder1
Gunzip files.tar.gz
mv file18 file17 (move the file18 to file17 and delete the file18 now)
To add the content of 2 filess into third file
Usage of RM command:
The `rm` command in Linux is used to remove files and directories. It's a powerful command, so be
very careful when using it as deleted files are usually not recoverable. Here are several ways to use
the `rm` command:
```
rm filename
```
```
rm example.txt
```
```
```
```
```
```
rm *.txt
```
This command will remove all files with a ".txt" extension in the current directory.
```
rm -r directory_name
```
For example, to delete a directory named `folder` and all its contents:
```
rm -r folder
```
```
rm -i filename
```
For example:
```
rm -i example.txt
```
```
rm -f filename
```
For example:
```
rm -f file.txt
```
7. Verbose Output
To show a more detailed output while deleting files, use the `-v` flag:
```
rm -v filename
```
For example:
```
rm -v example.txt
```
#CAT Command:
The cat command (short for concatenate) is used to display the contents of one or multiple files,
creating, or combining files.
Usage:
cat filea
This command displays the contents of the file named filename in the terminal.
bash
This command displays the contents of both file1 and file2 in the order listed.
2. less Command
The less command is a terminal pager that allows you to view and navigate through the contents of
a file.
Usage:
less filename
This command opens filename in a paginated view, allowing you to scroll up and down through the
content.
Basic Navigation:
Searching:
• Press / to start a forward search. Type your search term and press Enter.
• Press ? to start a backward search.
3. more Command
Similar to less, the more command is a terminal pager used to display the contents of a file one
screen at a time.
Usage:
more filename
Basic Navigation:
This command displays the content of the file filename one page at a time. Press the spacebar to
view the next page and press q to exit.
4. head Command
The head command is used to display the beginning lines of a file.
Usage:
head filename
This command shows the first ten lines of the file filename by default. You can also specify the
number of lines to display using the -n option. For example:
head -n 20 filename
5. tail Command
The tail command is used to display the ending lines of a file.
Usage:
tail filename
This command displays the last ten lines of the file filename by default. Similar to head, you can
specify the number of lines to display using the -n option. For example:
tail -n 15 filename
Remember, these commands are used to view or display the content of files in the terminal. They
don't modify the files themselves. They are useful for reading or quickly viewing the content of files
directly within the terminal.
Permissions in FileSystem:
Ubuntu (and other Linux-based systems), the chmod command is used to change the permissions of
files and directories. It allows users to modify the read, write, and execute permissions for the owner,
group, and others. Here's a detailed breakdown of how to use chmod:
Basic Permissions
Each file and directory in Linux has permissions represented by three categories: owner, group, and
others. These permissions can be set as read (r), write (w), and execute (x). These permissions are
represented numerically as well: read (4), write (2), and execute (1).
chmod Syntax
The chmod command follows this syntax:
who: Represents who the permission change will apply to and can be:
u (user/owner)
g (group)
o (others)
r (read) 4
w (write) 2
x (execute) 1
Usage Examples:
Changing File Permissions
Symbolic Mode:
To give the owner of the file read, write and execute permissions:
Numeric Mode:
Symbolic Mode:
To give the owner and group read, write, and execute permissions:
Numeric Mode:
CHGRP Command:
The `chgrp` command in Unix-like operating systems is used to change the group ownership of a file
or directory. Here's the basic syntax of the `chgrp` command:
```
```
- `GROUP`: The name of the group to which you want to change the ownership.
- `FILE`: The file or directory for which you want to change the group ownership.
Here are some common options used with `chgrp`:
- `-R` or `--recursive`: Recursively change the group ownership of directories and their contents.
#Examples:
```Syntax:
```
```Syntax
```
```Syntax
```
- They contain data, such as text files, binary files, and program executables.
```
$ ls -l
```
2. Directories:
- Directories contain lists of file names and their corresponding inode numbers.
```
$ ls -l
```
$ ls -l
```
- They are often used for accessing devices like hard drives.
```
$ ls -l
```
$ ls -l
```
```
$ ls -l
```
7. Sockets:
- Sockets are special files used for inter-process communication.
```
$ ls -l
```
8. Device Files:
- Device files represent physical and virtual devices on the system.
```
$ ls -l /dev/sda
```
These are the main file types in Linux, and they serve various purposes in the system. The output of
the `ls -l` command provides information about file permissions, ownership, size, and modification
time.
#Link Files in Linux:
In Linux, a "link" typically refers to two types of links: symbolic links (soft links) and hard links.
- Symbolic links can span different file systems and can link to files or directories that may not exist.
```bash
ln -s target_file link_name
```
Example:
```bash
ln -s /path/to/target/file /path/to/link
```
Example Output:
```bash
$ ls -l
```
2. Hard Links:
- A hard link is a reference to the same inode (data and disk block location) as the original file.
- Changes to the data in one file are reflected in all hard links.
- Hard links cannot span different file systems, and they must be on the same file system as the
original file.
Creating a hard link:
```bash
ln target_file link_name
```
Example:
```bash
ln /path/to/target/file /path/to/link
```
Example Output:
```bash
$ ls -l
```
It's important to note that when the last hard link to a file is deleted, the data is not immediately
deleted from the disk. It is only freed when there are no more references to the inode, which
happens when all links to the file are removed.
Practical:
You will see the change you are making by staying in the original location those changes are getting
reflected in the softlink as well
Also the changes you make in shortcuts are visible in the file1 present in original location.
If you delete the original file the softlink will also be deleted
HardLink:
• Content is still there in the Hardlink Fill even when we have deleted the original File
Locate Command:
The locate command is typically part of the findutils package on Unix-like systems. To use
the locate command, you need to ensure that this package is installed. The installation
process may vary depending on your operating system.
apt-get update
apt-get install findutils
apt install plocate
locate file21
locate file21 | grep “Hello”
-i is for case sensitive search