We manage linux using commands. Each command has its own purpose. We use an application called terminal to pass commands to the operating system.
How to open a terminal.?
Applications -> System Tools -> Terminal
Creating an empty file.
To create a file we use a command called "touch". When we create a file using touch, its file size will be zero.
Now we are going to create a file called file1.txt under the /root directory.
To create a file we use a command called "touch". When we create a file using touch, its file size will be zero.
Now we are going to create a file called file1.txt under the /root directory.
Listing the contents of a directory
How to confirm whether the file has been created.?To confirm it we need to list the contents of /root.
To list the contents of a directory we use a command called "ls"
How to confirm whether the file has been created.?To confirm it we need to list the contents of /root.
To list the contents of a directory we use a command called "ls"
Creating Multiple files:
To create multiple files, we need to specify each file name in touch command with spaces
We are going to create two files under /root with names file2.txt and file3.txt respectively.
To create multiple files, we need to specify each file name in touch command with spaces
We are going to create two files under /root with names file2.txt and file3.txt respectively.
Deleting a single file.
To delete a file we use the command "rm" . rm stands for remove. rm will ask you a confirmation whether you want to delete that file, then provide a "y" to delete that file.
We are going to Delete the file file3.txt from /root/ directory.
To delete a file we use the command "rm" . rm stands for remove. rm will ask you a confirmation whether you want to delete that file, then provide a "y" to delete that file.
We are going to Delete the file file3.txt from /root/ directory.
List the directory to confirm that the file is gone.
Deleting Multiple Files at once.
Just provide all the names of files with spaces to rm command.
Just provide all the names of files with spaces to rm command.
Creating a directory
To create a directory we use a command called "mkdir".
We are going to create a directory called backup1 under the /root. After creating the directory just list the contents of /root to confirm that the directory was created successfully.
To create a directory we use a command called "mkdir".
We are going to create a directory called backup1 under the /root. After creating the directory just list the contents of /root to confirm that the directory was created successfully.
Creating files under /root/backup1
Now we are going to create three files file1.txt,file2.txt,file3.txt under /root/backup1
Now we are going to create three files file1.txt,file2.txt,file3.txt under /root/backup1
Creating Multiple Directories.
creating dir1 under the /root
creating dir2 under the /root/backup
creating dir1 under the /root
creating dir2 under the /root/backup
Listing multiple directories at once.
What is a Working Directory.?
Working Directory is a directory where you are currently in. It means your current position in the file-system tree. When you type a command without a directory location; for example, creating a file with touch command without specifying any directory location, then the touch will assume that you want to create the file in the working directory. The same thing is applicable when you are creating, deleting files and directories etc.
How to see your Current Working Directory.?
To see the current working directory location just type the command "pwd". pwd stands for "Print Working Directory"
Working Directory is a directory where you are currently in. It means your current position in the file-system tree. When you type a command without a directory location; for example, creating a file with touch command without specifying any directory location, then the touch will assume that you want to create the file in the working directory. The same thing is applicable when you are creating, deleting files and directories etc.
How to see your Current Working Directory.?
To see the current working directory location just type the command "pwd". pwd stands for "Print Working Directory"
Understanding the Working Directory with some practical examples.
Create 3 files sample1.txt, sample2.txt and sample3.txt without specifying any destination directory.
Create 3 files sample1.txt, sample2.txt and sample3.txt without specifying any destination directory.
These files are created in our Working Directory ( Our Current Working Directory is "/root") . To confirm it we should list the contents of our current working directory . Just type "ls" with out any Directory name to list the contents of Working Directory.
Changing the Working Directory
To change directory we use a command called "cd". cd stands for change directory.
We are going to change the Working Directory to /root/dir1. After changing the directory just type the "pwd" command to confirm that the directory has been changed.
To change directory we use a command called "cd". cd stands for change directory.
We are going to change the Working Directory to /root/dir1. After changing the directory just type the "pwd" command to confirm that the directory has been changed.
what are switches ?
Switches are some alphabets like a, b, c, d. These helps to change the behaviour of a command. We use a "-" symbol before the switches. We shall see some practical examples below.
creating a hidden file.
To create a hidden file just put a "." at the beginning of the file name
we are going to create a file called secret.txt as hidden.
Switches are some alphabets like a, b, c, d. These helps to change the behaviour of a command. We use a "-" symbol before the switches. We shall see some practical examples below.
creating a hidden file.
To create a hidden file just put a "." at the beginning of the file name
we are going to create a file called secret.txt as hidden.
We are in the /root/dir1 directory, but when we list the contents of that directory we cannot find anything. You know why.? Its because we created secret.txt as a hidden file. By default ls command will not list any file names that starts with a dot. In order to list that file, we should use "-a" switch with ls. Here "a" is switch and "-" symbol must be put before any switch so as to consider it as a switch.
When we put "-a" with ls command, ls will list all files both hidden and normal files.
list all hidden file
When we put "-a" with ls command, ls will list all files both hidden and normal files.
list all hidden file
Creating a hidden directory.
Deleting a file without asking for a confirmation.
To delete a file without asking any questions we use a switch called "-f".
To delete a file without asking any questions we use a switch called "-f".
Delete a directory using rm command.
To delete a directory using rm command, we use "-r". But rm will still ask for the confirmation. To disable it we use "-f". So to delete a directory without any confirmation, we should use both "-r" and "-f". We can write the switches together as shown below.
To delete a directory using rm command, we use "-r". But rm will still ask for the confirmation. To disable it we use "-f". So to delete a directory without any confirmation, we should use both "-r" and "-f". We can write the switches together as shown below.
Various ways to List a Directory.
1. Long listing using "-l":
When you long list a directory, you get more details about the contents such as the type, size, modification time etc. There a few more details. We shall see that later.
If a line starts with a symbol "-" , it means that it is a file.
If a line starts with a symbol "d", it means that it is a directory.
1. Long listing using "-l":
When you long list a directory, you get more details about the contents such as the type, size, modification time etc. There a few more details. We shall see that later.
If a line starts with a symbol "-" , it means that it is a file.
If a line starts with a symbol "d", it means that it is a directory.
Different fields of "ls -l" Command
Use "-S" to sort the contents based upon their size. You should always use "-S" with "-l" because "-l" shows the size of file.
Listing hidden files using "-a" You should always use "-a" with "-l"
To convert the file size in to human readable form use "-h"
Use ‘-t’ option to sort the files by modified time (latest first).
Use ’1′ option to just list the files and folders (one per line) without any additional information