Linux Commands-1
> Linux commands
- Create a file
$ touch myfile
- Add contents in myfile
$ echo First Line > myfile
- Display the contents of myfile
$ cat myfile
- Add a new line to file. This will erase the old contents.
$ echo Second Line > myfile
$ cat myfile
- Append new text to the myfile
$ echo Third Line >> myfile
$ cat myfile
- Open myfile in a Texteditor
$ gedit myfile
- Add following contents in myfile
First Name:
Last Name:
House Name:
City :
Landmark :
District :
State :
Country :
Phone.No :
- Save the file
Linux Commands-2
- Command to list files in a directory
$ ls
- Command to list details of a file
$ ls -l
- Command to create a directroy - mkdir
- Create a sub-directory named 'myfolder'
$ mkdir myfolder
- Command to change to sub directory - cd
$ cd myfolder
- Print current working directory
$ pwd
- Command to change to parent directory
$ cd ..
- Create a copy of 'myfile' and name it as 'address'
$ cp myfile address
- Rename file 'address' as 'address.txt'
$ mv address address.txt
- Copy 'address.txt' to 'myfolder'
$ cp address.txt myfolder/
- Command to delete(or remove) a file - rm
- Delete 'myfile'
$ rm myfile
Linux Commands-3
- Remove a directory - rmdir
$ rmdir myfolder
For the above command to work, directory must be empty.
$ rm -r myfolder
More commands
--------------
- Lists the contents of root directory
$ ls /
- Lists all entries including hidden files and directories
$ ls -a
- This command will move to the user's home directory which is
"/home/username"
$ cd ~
- Copy the folder and subfolders from myfolder to myfolder1
$ cp -R myfolder myfolder1
- ln : command is used to create link to a file (or) directory.
$ ln -s myfile myfile.lnk
Creates a symbolic link to 'myfile' with the name of 'myfile.lnk'.
Here inode for 'myfile' and 'myfile.lnk' will be different.
- chown : command is used to change the owner of the file or
directory.
$ chown bpk myfile
The owner of the 'myfile' is root, change it to new user bpk
Note: This is an admin command, root user only can change the
owner of a file or directory.
Linux Commands-4
$ chown -R bpk myfolder
The owner of the 'myfolder' directory is root, With -R option the
files and
subdirectories of the user also gets changed.
- chgrp : command is used to change the group of the file or
directory.
$ chgrp bpk myfile
The group of 'myfile' is root. Change to newgroup bpk
$ chgrp -R bpk myfolder
The group of 'myfolder' directory is root. With -R, the files and
its subdirectories also changes
to new group bpk.
Note: This is an admin command. Root user only can change the
group of the file or directory.
- chmod : command allows you to change access rights to files and
directories.
File Permissions
-----------------
# File Permission
0 none
1 execute only
2 write only
3 write and execute
4 read only
5 read and execute
6 read and write
7 set all permissions (read, write and execute)
Note: File Permission is given for users, group and others.
- To view your files with what permission they are:
$ ls -al
Linux Commands-5
- To allow everyone(owner, group and others) to read, write, and
execute the file
$ chmod 777 myfile
- To make a file readable and writable by the group and others
$ chmod 066 myfile
- Create directory and set permissions
$ mkdir -m 666 myfolder
The above command is used to create the directory 'myfolder' and
set the read and write permission.
- mv : command which is short for move.
It is used to move/rename file from one directory to another.
$ mv myfile myfile.txt
Note: mv command is different from cp command as it completely
removes the file from the
source and moves to the directory specified, where cp command just
copies the content from
one file to another.
- To move a directory
$ mv myfolder myfolder1
- To Move multiple files into another directory
$ mv myfile myfile.txt myfolder
Note: This command moves the files myfile and myfile.txt from the
current directory to myfolder.
------- END --------