All data in Linux is organized into files. All files are organized into directories. These directories are organized into a tree like structure called the file system. When you are working with Linux, you spend most of your time working with files. In Linux there are three basic types of files.
1. Ordinary Files
An ordinary file is a file on the system that contains data, text, or program instructions.
2. Directories
Directories store both special and ordinary files. For users familiar with Windows, Linux directories are equivalent to folders.
3. Special Files
Some special files provide access to hardware such as hard drives, CD-ROM drives, modems, and Ethernet adapters.(device file) Other special files are similar to aliases or shortcuts and enable you to access a single file using different names. (link file)
ls command list the files and directories stored in the current directory. If you pass file argument to ls it
return the name of the file if it exist in current directory otherwise echo error message.If you pass the directory
name to ls command, and directory exist then ls list all the files and directories in that direcotry. If the
directory doesn't exist then it prints the error message.
$ ls
Lab Notes README.md
$ ls README.md
README.md
$ ls abc
ls: cannot access 'abc': No such file or directory
$ ls Lab
'Week 01' 'Week 02' 'Week 03'
$ ls -l
total 12
drwxr-xr-x 5 ram ram 4096 Apr 29 22:28 Lab
drwxr-xr-x 2 ram ram 4096 May 6 01:02 Notes
-rw-r--r-- 1 ram ram 2291 Apr 29 22:28 README.md
Here is the information about all the listed columns
-
First column Represents the file type and the permission given on the file. First character of the first column represents the file type. Following are the different file types:
-: Regular File : Such as ASCII text file, binary executable or hard link.b: Block Spec : Raw I/O Device File such as physical hard drive.c: Character Special File : Raw I/O Device File such as physical Keyboard.d: Directory File : That contains other file and directories.l: Symbolic Link File : Links on any regular file.p: Named Pipe : A mechanism for inteprocess communication.s: Socket : Uses for interprocess communincation.
-
Second Column Represents the number of memory blocks taken by the file or directory.
-
Third Column Represents the owner of the file. This is the Unix user who created this file.
-
Fourth Column Represents the group of the owner. Every Unix user will have an associated group.
-
Fifth Column Represents the file size in bytes.
-
Sixth Column Represents the date and the time when this file was created or modified for the last time.
-
Seventh Column Represents the file or the directory name.
$ ls -t
Notes README.md Lab
$ ls -1
Lab
Notes
README.md
$ ls -s
total 12
4 Lab 4 Notes 4 README.md
$ ls -lh
total 12K
drwxr-xr-x 5 ram ram 4.0K Apr 29 22:28 Lab
drwxr-xr-x 2 ram ram 4.0K May 6 01:02 Notes
-rw-r--r-- 1 ram ram 2.3K Apr 29 22:28 README.md
$ ls -sh
total 12K
4.0K Lab 4.0K Notes 4.0K README.md
/for directory- nothing for normal file
@for link file*for executable file.
$ ls -F
abc Desktop/ examples.desktop Pictures/ temp/ Videos/
abc.sh* Documents/ fastai/ Public/ Templates/
anaconda3/ Downloads/ Music/ snap/ usr/
* -a shows all the hidden files and directories.
Name of hidden file or directory in Linux start with .. -a option also display the current directory
represented by . and parent directory represented by ... To hide these files to display use ls -A command.
$ ls -a
. .. .git .gitignore Lab Notes README.md
$ ls -A
.git .gitignore Lab Notes README.md
$ ls -R
.:
Lab Notes README.md
./Lab:
'Week 01' 'Week 02' 'Week 03'
'./Lab/Week 01':
ex01.md ex02.md ex03.md ex04.md ex05.md img
'./Lab/Week 01/img':
fs001.png
'./Lab/Week 02':
ex01.md exercise5.md img
'./Lab/Week 02/img':
fs001.png fs002.png
'./Lab/Week 03':
exercise1.md exercise2.md exercise3.md exercise4.md files
'./Lab/Week 03/files':
logfile titanic
./Notes:
'01. Introduction to Linux OS.md' '02. bc Linux Calculator'
cat (concatenate ) command is very frequently used in linux. It reads data from file and give their content as
output. It helps us to create, view and concatenate files.
$ cat file1
#include<stdio.h>
int main(){
printf "hello world.\n";
return 0;
}
$ cat file1 file2
#include<stdio.h>
int main(){
printf "hello world.\n";
return 0;
}
#include<iostream>
using namespace std;
int main(){
cout << "hello world." << endl;
return 0;
}
$ cat -n file1
1 #include<stdio.h>
2 int main(){
3
4 printf "hello world.\n";
5 return 0;
6
7 }
8
$ cat -E file1
#include<stdio.h>$
int main(){$
$
printf "hello world.\n";$
return 0;$
$
}$
$
$ cat file
one
two
three
$ cat -s file1
one
two
three
$tac file1
}
return 0;
printf "hello world.\n";
int main(){
#include<stdio.h>
We can create a file using the cat command. To create a file use cat > file_name command and then start typing the
content of the file. When finish press ctrl + d key.
$ cat > foo
one
two
three
[ctrl+d]
$ cat foo
one
two
three
To copy the file use > operator. > operator overwrite the content of destination file. If you want to append the
content use >> operator.
$ cat file1 > abc
$ cat abc
hello my name is ram.
$ cat file2 > abc
$ cat abc
welcome to ceit.
$ cat file1 > abc
$ cat abc
hello my name is ram.
$ cat file2 >> abc
$ cat abc
hello my name is ram.
welcome to ceit.
The touch command is a standard command used in UNIX/Linux operating system. touch command is used to create, change and modify timestamps of a file.
- You can create a single file at a time using
touchcommand.
$ touch file1
$ls
file1touchcommand can be used to create the multiple numbers of files at the same time.
$ touch file2 file3 file4
$ls
file1 file2 file3 file4- To change or update the last access or modification times of a file
touch -acommand is used.
$ ls -l file1
-rw-r--r-- 1 ram ram 0 May 6 21:57 file1
$ touch file1
$ ls -l file1
-rw-r--r-- 1 ram ram 0 May 6 21:59 file1