0% found this document useful (0 votes)
11 views9 pages

Cmds 1

The document provides an overview of basic Linux commands for file and directory manipulation, including listing files with 'ls', creating and viewing files with 'cat', and deleting files with 'rm'. It also covers directory management using 'mkdir' and 'rmdir', as well as changing file permissions with 'chmod'. Additional commands such as 'man', 'history', and 'clear' are mentioned for reference and usability.

Uploaded by

dagmawitlegesse6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

Cmds 1

The document provides an overview of basic Linux commands for file and directory manipulation, including listing files with 'ls', creating and viewing files with 'cat', and deleting files with 'rm'. It also covers directory management using 'mkdir' and 'rmdir', as well as changing file permissions with 'chmod'. Additional commands such as 'man', 'history', and 'clear' are mentioned for reference and usability.

Uploaded by

dagmawitlegesse6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

df
2. ls
3. ping

https://www.guru99.com/linux-
redirection.htmlListing files (ls)
If you want to see the list of files on your UNIX or Linux system, use the 'ls' command.

It shows the files /directories in your current directory.

Note:

 Directories are denoted in blue color.


 Files are denoted in white.
 You will find similar color schemes in different flavors of Linux.

Suppose, your "Music" folder has following sub-directories and files.

You can use 'ls -R' to shows all the files not only in directories but also subdirectories

NOTE: The command is case-sensitive. If you enter, "ls - r" you will get an error.

'ls -al' gives detailed information of the files. The command provides information in a columnar
format. The columns contain the following information:

1st Column File type and access permissions


2nd Column # of HardLinks to the File
3rd Column Owner and the creator of the file
4th Column Group of the owner
5th Column File size in Bytes
6th Column Date and Time
7th Column Directory or File name

Let's see an example -

Listing Hidden Files

Hidden items in UNIX/Linux begin with - at the start, of the file or directory.

Any Directory/file starting with a '.' will not be seen unless you request for it. To view hidden
files, use the command.

ls -a

Creating & Viewing Files


The 'cat' command is used to display text files. It can also be used for copying, combining and
creating new text files. Let's see how it works.

To create a new file, use the command

1. cat > filename


2. Add content
3. Press 'ctrl + d' to return to command prompt.

To view a file, use the command -

cat filename

Let's see the file we just created -

Let's see another file sample2


The syntax to combine 2 files is -

cat file1 file2 > newfilename

Let's combine sample 1 and sample 2.

As soon as you insert this command and hit enter, the files are concatenated, but you do not see a
result. This is because Bash Shell (Terminal) is silent type. It will never give you a
confirmation message like "OK" or "Command Successfully Executed". It will only show a
message when something goes wrong or when an error has occurred.

To view the new combo file "sample" use the command

cat sample

Note: Only text files can be displayed and combined using this command.

Deleting Files
The 'rm' command removes files from the system without confirmation.

To remove a file use syntax -

rm filename

Moving and Re-naming files


To move a file, use the command.

mv filename new_file_location

Suppose we want to move the file "sample2" to location /home/guru99/Documents. Executing


the command

mv sample2 /home/guru99/Documents
mv command needs super user permission. Currently, we are executing the command as a
standard user. Hence we get the above error. To overcome the error use command.

sudo command_you_want_to_execute

Sudo program allows regular users to run programs with the security privileges of the superuser
or root.

Sudo command will ask for password authentication. Though, you do not need to know the root
password. You can supply your own password. After authentication, the system will invoke the
requested command.

Sudo maintains a log of each command run. System administrators can trackback the person
responsible for undesirable changes in the system.

guru99@VirtualBox:~$ sudo mv sample2 /home/quru99/Documents


[sudo] password for guru99: ****
guru99@VirtualBox:~$

For renaming file:

mv filename newfilename

NOTE: By default, the password you entered for sudo is retained for 15 minutes per terminal.
This eliminates the need of entering the password time and again.

You only need root/sudo privileges, only if the command involves files or directories not owned
by the user or group running the commands

Directory Manipulations

Enough with File manipulations! Let's learn some directory commands.

Creating Directories

Directories can be created on a Linux operating system using the following command

mkdir directoryname

This command will create a subdirectory in your present working directory, which is usually
your "Home Directory".
For example,

mkdir mydirectory

If you want to create a directory in a different location other than 'Home directory', you could use
the following command -

mkdir

For example:

mkdir /tmp/MUSIC

will create a directory 'Music' under '/tmp' directory

You can also create more than one directory at a time.

Removing Directories
To remove a directory, use the command -

rmdir directoryname

Example

rmdir mydirectory

will delete the directory mydirectory

Tip: Ensure that there is no file / sub-directory under the directory that you want to delete. Delete
the files/sub-directory first before deleting the parent directory.

Renaming Directory
The 'mv' (move) command (covered earlier) can also be used for renaming directories. Use the
below-given format:

mv directoryname newdirectoryname

Let us try it:

Other Important Commands


The 'Man' command
Man stands for manual which is a reference book of a Linux operating system. It is similar to
HELP file found in popular software.

To get help on any command that you do not understand, you can type

man

The terminal would open the manual page for that command.

For an example, if we type man man and hit enter; terminal would give us information on man
command

The History Command


History command shows all the commands that you have used in the past for the current terminal
session. This can help you refer to the old commands you have entered and re-used them in your
operations again.

The clear command


This command clears all the clutter on the terminal and gives you a clean window to work on,
just like when you launch the terminal.
Changing file/directory permissions with 'chmod' command
Say you do not want your colleague to see your personal images. This can be achieved by
changing file permissions.

We can use the 'chmod' command which stands for 'change mode'. Using the command, we can
set permissions (read, write, execute) on a file/directory for the owner, group and the world.
Syntax:

chmod permissions filename

There are 2 ways to use the command -

1. Absolute mode
2. Symbolic mode

Absolute(Numeric) Mode
In this mode, file permissions are not represented as characters but a three-digit octal
number.

The table below gives numbers for all for permissions types.

Number Permission Type Symbol

0 No Permission ---

1 Execute --x

2 Write -w-

3 Execute + Write -wx

4 Read r--

5 Read + Execute r-x

6 Read +Write rw-

7 Read + Write +Execute rwx


Let's see the chmod command in action.

In the above-given terminal window, we have changed the permissions of the file 'sample to
'764'.

'764' absolute code says the following:

 Owner can read, write and execute


 Usergroup can read and write
 World can only read

This is shown as '-rwxrw-r-

This is how you can change the permissions on file by assigning an absolute number.

Symbolic Mode
In the Absolute mode, you change permissions for all 3 owners. In the symbolic mode, you can
modify permissions of a specific owner. It makes use of mathematical symbols to modify the file
permissions.

Operator Description

+ Adds a permission to a file or directory

- Removes the permission

Sets the permission and overrides the permissions


=
set earlier.

The various owners are represented as -

User Denotations

u user/owner

g group

o other

a all
We will not be using permissions in numbers like 755 but characters like rwx. Let's look into an
example

You might also like