Basic SHELL Commands
Basic SHELL Commands
To display shell
echo $SHELL
To change your shell with chsh:
1.cat /etc/shells
At the shell prompt, list the available shells on your system with
cat /etc/shells.
2.chsh
Enter chsh (for "change shell").
Some systems prompt for a password, and some don't.
3./bin/zsh
Type in the path and name of your new shell.
4.su - yourid
Type in su - and your userid to relog in to verify that everything
works correctly.
If it doesn't, use chsh again and change back to the original shell or
to a different one.
General command format
SYNOPSIS
cal [-smjy13] [[[day] month] year]
DESCRIPTION
Cal displays a simple calendar. If arguments are not specified, the cur-
rent month is displayed. The options are as follows:
– Read (4)
– Write (2)
– Execute (1)
• rmdir
• rmdir command will delete the empty directories.
• i.e directory without any sub-directories or files:
• You can’t remove a subdirectory unless you are placed in
a directory which is hierarchically above the one you have
chosen to remove.
• $ rmdir test
• Example-2:
• To Delete Nested Empty Directories in Linux:
• $ rmdir -p dir1/dir2/dir
touch command-
Touch options expression filename(s)
• touch abc – creates file abc if does not exist. Both times
are set to current time
• touch abc pqr ----creates two files abc and pqr
• stat filename to view complete timestamps of file
cat command-
It has three related functions with regard to text files:
displaying them, combining copies of them and creating new ones.
cat's general syntax is
cat [options] [filenames]
Reading Files
• The most common use of cat is to read the contents of files
cat file1
• In the following example, the standard output of cat is redirected
using the output redirection operator to file2:
cat file1 > file2
That is, the output from cat is written to file2 instead of being
displayed on the monitor screen.
• Following command will concatenate copies of the contents of the
three files file1, file2 and file3:
cat file1 file2 file3
• The output can be redirected using the output redirection
operator to another file, such as file4, using the following:
cat file1 file2 file3 > file4
• In the next example, the output of cat is piped to the sort filter
in order to alphabetize the lines of text after concatenation and
prior to writing to file4:
cat file1 file2 file3 | sort > file4
• >> operator is used to append contents to file.
cat >> file1
• cat –n file1 ---- displays line numbers with text
• ls – list directory contents.
• ls [options] [file|dir]
• ls –l shows file or directory, size, modified date and
time, file or folder name and owner of file and it’s
permission.
• # ls -l
• drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music
• drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures
description
Example-2:
To print value of x, where x=10.
$ echo $x
output:
10
echo the STRING(s) to standard output.
Example-:
To echo output to a file and not standard output. in below example
output is redirected to test file.
$ echo "Hello World!" > test
output:
$cat test
Hello World!
cp-Copy from source to dest
cp [options] src dest
cp stands for copy. This command is used to copy files or group of
files or directory. It creates an exact image of a file on a disk with
different file name. cp command require at least two filenames in its
arguments.
Two file names : If the command contains two file names, then it
copy the contents of 1st file to the 2nd file. If the 2nd file doesn’t
exist, then first it creates one and content is copied to it. But if it
existed then it is simply overwritten without any warning.
cp abc.txt pqr.txt
cp –i abc.txt pqr.txt
One or more arguments : If the command has one or more
arguments, specifying file names and following those arguments, an
argument specifying directory name then this command copies each
source file to the destination directory with the same name, created if
not existed but if already existed then it will be overwritten.
Absolute permissions:
Read by owner only
$ chmod 400 sample.txt
Write by group only
$ chmod 020 sample.txt
Execute by anyone
$ chmod 001 sample.txt
Allow everyone to read, write, and execute file.
$ chmod 777 sample.txt
• Relative permissions:
• user/owner (u), other users in the file's group (g), other users not in
the file's group (o), or all users (a).
• The operator '+' causes the permissions selected to be added to the
existing permissions of each file;
• '-' causes them to be removed;
• and '=' causes them to be the only permissions that the file has.
Deny execute permission to everyone.
$ chmod a-x sample.txt
Allow read permission to everyone.
$ chmod go+rw sample.txt
Make a shell script executable by the user/owner.
$ chmod u+x samplescript.sh
chmod ug+x,o-w a.txt
wc word count
rm - remove files or directories
rm [OPTION]... FILE...
rm removes each specified file. By default, it does not remove
directories.
Example-1:
Remove the file myfile.txt. If the file is write-protected, you will be
prompted to confirm that you really want to delete it:
$ rm myfile.txt
rm myfile.txt myfile1.txt
rm -i myfile.txt
Example-2:
Remove the file myfile.txt. You will not be prompted, even if the file
is write-protected; if rm can delete the file, it will:
$ rm -f myfile.txt
Example-3:
Remove all files in the working directory. If it is write-protected, you will be
prompted before rm removes it:
$ rm *
Example-4:
Remove all files in the working directory. rm will not prompt you for any
reason before deleting them:
$ rm -f *
Example-5:
Attempt to remove every file in the working directory, but prompt before
each file to confirm:
$ rm -i *
Example-7:
Remove the directory mydirectory, and any files and directories it
contains. If a file or directory that rm tries to delete is write-
protected, you will be prompted to make sure that you really want
to delete it:
$ rm -r mydirectory
Example-8:
Same as the above command, but you will never be prompted; if
rm can delete the files, it will:
$ rm -rf mydirectory
•
Pipes
$ cut -f 2 -d \; sample.txt
2
head and tail
"head" will display information from the beginning of the file and "tail" will display
information from the end of the file.
By default both the "head" and "tail" program display the first 10 or last 10 lines
respectively:
head concatfile2.txt
head -n 5 concatfile2.txt
1 Linux Mint
2 Mageia
3 Ubuntu
4 Fedora
5 openSUSE
tail -n 5 concatfile2.txt
26 Debian
27 Arch Linux
28 PCLinuxOS
29 Zorin OS
30 CentOS
[it@localhost ~]$ cat try.txt uniq command
this is file
this is file
contents are
closing file
closing file
[it@localhost ~]$ uniq try.txt shows data without duplication
this is file
contents are
closing file
[it@localhost ~]$ uniq -d try.txt shows duplicate lines only
this is file
closing file
[it@localhost ~]$ uniq -u try.txt shows non duplicate lines only
contents are
[it@localhost ~]$ uniq -c try.txt shows count of duplication and contents of file
2 this is file
1 contents are
2 closing file
[it@localhost ~]$
Linux mv command.
mv command is used to move files and directories.
It renames a file or directory.
$ mv [options] source dest