Basic Linux Commands
1. pwd Command
It stands for print working directory, It simply print the absolute path name of your current working directory
example-pwd
2. cd command
Change directory and it simply change your current directory to this Desktop directory
example-cd /Directory_path
cd /home/goeduhub/Desktop
cd / Changes to your root directory
cd ~ Changes to your home directory
cd .. Changes to your parent directory
3. ls : list directory contents
(The ls command will show you the list of files in your current directory.)
# ls
4. cp Command
The cp command copies files or directories. It can be used in two different ways:
Way #1
cp file1 file2
This will make a copy of file1 named file2. cp R dir1 dir2 This will make a copy of dir1 named dir2 (Assuming that dir2 didn't exist)
Way #2
cp file1 file2 ..... destination (directory) (Must exist) This will copy multiple files (file1, file2, ... ) into the destination directory.
cp R dir1 dir2 ..... destination (directory) (Must exist) This will copy multiple directories into the destination directory
5. Make a directory
If you want to create a directory, then you use mkdir command as follows
mkdir directoryname
You can create more than one directory as follows
mkdir directory1 directory2 directory3
This will create three directories
mkdir stands for make directory
As you can see, Linux command names are logical. And that's why they are so easy to remember
6. Remove an empty directory
Now if you want to delete an empty directory (For example, directory1) Then you use rmdir command as follows
rmdir directory1
This will only remove directory1 if it's empty ! You can also remove more than one empty directory at once. (For example, directory1 directory2 directory3)
rmdir directory1 directory2 directory3
This will only remove directory1, directory2 and directory3 if they are all empty . However, if directory1 and directory2 are non empty and directory3 is empty. Then the above command will only delete directory3 and so on. Basically, It just deletes the empty directories.
rmdir stands for remove directory
7. Touch Command
The touch command is the easiest way to create new, empty files. If you want to create an empty file (not a directory) then you just type
touch yourfilename
You can also create multiple files at the same time. For example
touch file1 file2 file3
will create 3 new empty files named file1,file2 and file3 respectively. Another use for the touch command touch is also used to update the timestamp (Modification date) for an existing file. For example if you already have a file named oldfile then
touch oldfile will change the timestamp of oldfile to the current time. Similarily touch oldfile1 oldfile2 oldfile3 will change the timestamp of oldfile1,oldfile2 and oldfile3 to the current time.
Command Utility
Listing files inside a directory-
ls -l List the files and directories in the current directory in long (table) format (It is recommended to
use -l with ls for better readability).
ls -ld dir-name List information about the directory dir-name instead of its contents.
ls -a List all the files including the hidden ones (File names starting with a . are hidden files in Linux).
ls -F Appends a symbol at the end of a file name to indicate its type (* means executable, / means directory, @ means symbolic link, = means socket, | means named pipe, > means door).
ls -lt List the files sorted by last modified time with most recently modified files showing at the top (remember -l option provides the long format which has better readability).
ls -lh List the file sizes in human readable format.
ls -lR Shows all subdirectories recursively.
tree Will generate a tree representation of the file system starting from the current directory.
File/directory create, copy and remove
Command Utility
cp -p source destination
Will copy the file from source to destination. -p stands for preservation. It
preserves the original attributes of file while copying like file owner, timestamp,
group, permissions etc.
cp -R source_dir destination_dir
Will copy source directory to specified destination recursively.
mv file1 file2
In Linux there is no rename command as such. Hence mv moves/renames the
file1 to file2.
rm -i filename
Asks you before every file removal for confirmation. IF YOU ARE A NEW USER
TO LINUX COMMAND LINE, YOU SHOULD ALWAYS USE rm -i. You can specify
multiple files.
rm -R dir-name
Will remove the directory dir-name recursively.
rm -rf dir-name
Will remove the directory dir recursively, ignoring non-existent files and will
never prompt for anything. BE CAREFUL USING THIS COMMAND! You can
specify multiple directories.
rmdir dir-name
Will remove the directory dir-name, if it's empty. This command can only remove
empty directories.
mkdir dir-name
Create a directory dir-name.
mkdir -p dir-name/dir-name
Create a directory hierarchy. Create parent directories as needed, if they don't
exist. You can specify multiple directories.
File/directory permissions and groups
Command Utility
chmod <specification> filename
Change the file permissions. Specifications = u user, g group, o other, + add
permission, - remove, r read, w write,x execute.
chmod -R <specification> dirname
Change the permissions of a directory recursively. To change permission of
a directory and everything within that directory, use this command.
chmod go=+r myfile
Add read permission for the owner and the group.
chmod a +rwx myfile
Allow all users to read, write or execute myfile.
chmod go -r myfile
Remove read permission from the group and others.
chown owner1 filename
Change ownership of a file to user owner1.
chgrp grp_owner filename
Change primary group ownership of file filename to group grp_owner.
chgrp -R grp_owner dir-name
Change primary group ownership of directory dir-name to group grp_owner
recursively. To change group ownership of a directory and everything within
that directory, use this command.