Skip to content

Latest commit

 

History

History
382 lines (255 loc) · 8.28 KB

File metadata and controls

382 lines (255 loc) · 8.28 KB

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)

1. ls

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'

options

* –l option allow you to get more information about the listed file and directories.

			$ 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

  1. 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.
  2. Second Column Represents the number of memory blocks taken by the file or directory.

  3. Third Column Represents the owner of the file. This is the Unix user who created this file.

  4. Fourth Column Represents the group of the owner. Every Unix user will have an associated group.

  5. Fifth Column Represents the file size in bytes.

  6. Sixth Column Represents the date and the time when this file was created or modified for the last time.

  7. Seventh Column Represents the file or the directory name.

* -t options sort file and directory by modification time. Newest modified file appear first.

		$ ls -t
		Notes  README.md  Lab

* -1 options allow ls to list one file per line.

		$ ls -1
		Lab
		Notes
		README.md

* -s option print the allocated size of each file, in blocks.

		$ ls -s
		total 12
		4 Lab  4 Notes  4 README.md

* -h with -l and -s, print sizes like 1K 234M 2G etc in human readable form.

		$ 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
		

* -F classifies the file with different special character for different kind of files.

  • / 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

* -R list subdirectories recursively.

		$ 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'

2. cat

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.

a. To view a single file

		$ cat file1
		#include<stdio.h>
		int main(){

			printf "hello world.\n";
			return 0;

		}

b. To view multiple files

		$ 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;
		}

c. To view contents of a file preceding with line numbers use -n option.

	     $ cat -n file1
	     1	#include<stdio.h>
	     2	int main(){
	     3	
	     4		printf "hello world.\n";
	     5		return 0;
	     6	
	     7	}
	     8	

d. To highlight the end of line use -E option.

		$ cat -E file1
		#include<stdio.h>$
		int main(){$
		$
			printf "hello world.\n";$
			return 0;$
		$
		}$
		$

e. To suppress repeated empty lines in output use -s option.

		$ cat file
		one



		two
		three

		$ cat -s file1
		one

		two
		three

f. cat command can display content in reverse order using tac command.

		$tac file1

		}

			return 0;
			printf "hello world.\n";

		int main(){
		#include<stdio.h>

g. To create a file

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
		

h. Copy the contents of one file to another file.

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.

3. touch

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 touch command.
		$ touch file1
		$ls
		file1
  • touch command 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 -a command 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

References: