Documente Academic
Documente Profesional
Documente Cultură
CAT
escription
The cat command reads one or more files and prints their contents to
standard output. Files are read and output in the order they appear in the
command arguments.
Syntax
The syntax for the cat command is:
Option Description
Each tab will display as ^I and each form feed will display as ^L. (May require
-t
the -v option)
NOTE: While the options provided here work on most UNIX systems,
some UNIX flavors may have changed their meanings or uses. If you
experience an incompatibility with these options, please consult
the cat manual page (see man command) on your system for a list of
compatible options.
files
A list of file names separated by spaces that cat will concatenate the
contents of.
Operators
The following operators can be used with the cat command:
Operator Description
Redirect the output of the cat command to a file rather than standard output. The
>
destination file will be created if it doesn't exist and will be overwritten if it does.
Append the output of the cat command to the end of an existing file. The destination
>>
file will be created if it doesn't exist.
Send (or pipe) the output of the cat command into another command for further
|
processing.
Applies To
Most UNIX variants including: FreeBSD, OpenBSD, Solaris, Illumos,
SmartOS
Type of Command
System Executable
cat file1
In this screenshot, you can see that the contents of file1 are displayed as
expected.
Example - Using cat to output the contents of two files to the
display
Building on the previous example, we will use the cat command to output
the contents of two files to the display.
For this example the file named file1 contains the text: Learning cat with
TechOnTheNet is fun! and the file named file2contains the
text: Concatenating two files into one is even more fun.
The following command outputs the contents of the files file1 and file2 to
the display.
In this screenshot, you can see that the contents of file1 is displayed first
followed by the contents of file2.
Example - Using cat to redirect the contents of two files to
another file
In this example we will use the cat command to redirect the contents of two
files into another file.
The file named file1 contains the text: Learning cat with TechOnTheNet is
fun! and the file named file2 contains the text: Concatenating two files into
one is even more fun.
The following command redirects the contents of the files
named file1 and file2 to the file named all.
To view the contents of the file named all we will also use the cat command
as we did in the first example.
cat all
As we can see in the following screenshot, the contents of the files
named file1 and file2 are sent into the file named all by the first command
and the contents of the file all is output to the display by the second
command.
To view the contents of the file named file2 we will use the following cat
command:
cat file2
This screenshot shows the contents of file2 after the contents of file1 were
appended to the end of file2.
The line Learning cat with TechOnTheNet is fun! from the file
Description
The change directory (cd) command is built into the system shell and changes the
current working directory. The cd command can be used to either chan
Syntax
The syntax for the cd command is:
cd <directory>
chmod [option] mode files
Who u=user, g=group, o=other, a=all (default) Who u=user, g=group, o=other, a=all (default)
+ means add permission + means add permission
- means remove permission - means remove permission
Opcode Opcode
= means assign permission and remove the = means assign permission and remove the
permission of unspecified fields permission of unspecified fields
r=Read, w=write, x=Execute, s=set uid bit, r=Read, w=write, x=Execute, s=set uid bit,
Permission t=sticky bit Permission t=sticky bit
u=user, g=group, o=other, l=mandatory locking u=user, g=group, o=other, l=mandatory locking
Example Example
Who u=user, g=group, o=other, a=all (default) Who u=user, g=group, o=other, a=all (default)
r=Read, w=write, x=Execute, s=set uid bit, r=Read, w=write, x=Execute, s=set uid bit,
Permission t=sticky bit Permission t=sticky bit
u=user, g=group, o=other, l=mandatory locking u=user, g=group, o=other, l=mandatory locking
Example Example
Example
Advertisement
chmod
The chmod command can be used in a couple of different ways, with permissions (or modes) set by numbers or by letters.
Permissions can be given to a user who owns the file (u = user), group of said user (g = group), everyone else (o = others) or all
users (a). And the basic permissions that can be given include read (r), write (w), and execute (x). There are also X, s, and t, but
they're less commonly used.
When using numbers you can use a numeric value such as 644 to set permissions. The position of the value represents to whom is
the permission given, and the actual value represents which (or how much) permissions are given as a sum total of each
permission's unique value.
First position (in the above example 6) refers to the user. Second refers to the group of the user, and the third refers to all others.
So a value of 4 will only give read rights, but a value of 6 will give read and write rights because it is a sum of 4 and 2. 5 will
give only read ane execute rights, and 7 will give all rights. Do this calculation for each numerical position and you'll end up with
the desired value. So in the example of 644 we're giving the user who owns the file the permission to read and write (but not
execute), the group of that user the permission to read only, and others the right to read only as well.
To set this mode with chmod on a file called important.txt we would simply run this command:
Note that making a file executable, if it were a script or a program, amounts to simply giving someone or everyone a permission
to execute. If this was an important.sh bash script we could allow the owner to execute, and others to read with the 744 mode, or
everyone to execute with 755.
Now, we can also use letters to accomplish the same thing, and we've already mentioned the relevant letters above. This is
probably easier to remember than using numbers. For example, to accomplish the 644 permissions above we would run this:
So we're saying file owner user gets read and write permissions, group and others get to read.
The second example, with the important.sh file being made executable we could just run this:
If important.sh already had permissions set to 644 we can add everyone execute rights by simply running:
chmod +x important.sh
Not
yntax:
grep [options] pattern [files]
Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
with each such part on a separate output line.
Sample Commands
Consider the below file as an input.
$cat > geekfile.txt
unix is great os. unix is opensource. unix is free os.
Output:
2. Displaying the count of number of matches : We can find the number of lines that matches the given
string/pattern
$grep -c "unix" geekfile.txt
Output:
2
3. Display the file names that matches the pattern : We can just display the files that contains the given
string/pattern.
$grep -l "unix" *
or
Output:
geekfile.txt
Output:
unix is great os. unix is opensource. unix is free os.
Output:
unix
unix
unix
unix
unix
unix
6. Show line number while displaying the output using grep -n : To show the line number of file with the line
matched.
$ grep -n "unix" geekfile.txt
Output:
1:unix is great os. unix is opensource. unix is free os.
Output:
learn operating system.
Unix linux which one you choose.
Output:
unix is great os. unix is opensource. unix is free os.
11. -f file option Takes patterns from file, one per line.
$cat pattern.txt
Agarwal
Aggarwal
Agrawal
$grep –f pattern.txt geekfile.txt
This article is contributed by Akshay Rajput. If you like GeeksforGeeks and would like to
contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to
contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main
page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more
information about the topic discussed above.
Article Tags :
Articles
Linux-Unix
linux-command
Test Engineer
I am writing this blog for people who want to explore software testing as
Classic
Flipcard
Magazine
Mosaic
Sidebar
Snapshot
Timeslide
How to count no. of links on webpage and print using selenium2 using java
$ ls command is used for directory and file listing with there permission.
Directories commands
$ pwd -command is used for knowing user current working directory or you can say print working directory.
$ cd - similar to DOS cd .it is use to change directory.
$ More filename -List the contents of file pausing after each screen (press Space to continue listing).
$ less filename - Display filename one screenful.A pager similar to (better than) more.
The control characters that have special meanings in a UNIX environment. Denoted by the character ^ followed by a letter, each control
character can be generated by holding down Control and pressing the specified letter, then releasing both keys.
^c
Interrupt a process (applicable for most processes); used to halt a process immediately.
Pressing ^c, for example, enables you to stop the listing of a long file. However, if the file is very long, there may be a noticeable time delay before the
process is actually interrupted.
^d
Terminate a process; end-of-file marker (EOF). If a process does not have a clean termination command and pressing ^c does not work, try pressing
^d.
^s
Suspend terminal output to your system. To resume terminal output, press ^q. ^s is usually referred to as XON.
To view text a page at a time, use the more command.
^q
Resume terminal output suspended by pressing ^s (see above). ^q is usually referred to as XOFF.
^z
Suspend the foreground process, that is, place the current active program in a paused state.
Type the fg command to reactivate a process in the foreground. A foreground process will usually keep control of your keyboard and terminal.
Type the bg command to reactivate a process in the background. A background process will let you type other commands on the keyboard while it is
running in a detached state.
^u
Erase previous word on a UNIX command line. A word is a string of characters separated by a space or tab.
OTHER COMMANDS
$ print file -Send file to the UNIX laser printer.
$ man command - Display information about command.
$ apropos keyword - List commands/programs that contain keyword in their description.
$ du - List the number of Kbytes for each directory below current working directory.
$ talk username - Interactively communicate with username; username has to be logged-in.
$ finger username - Find the username for username.
$ w - Display current activity on the system.
$ who - Show users that are currently logged-in.
$ tin - A program to read a collection of newsgroups (new articles arrive daily).
$ help [command] - If specified without command, a brief introduction is listed.If command is specified, help displays the man page for command.
$ pine - Send email through the Internet
$ quota –v - Display disk usage and disk quota. This command does not display your email quota.
$ mydiskspace - Display uhunix disk usage only. This command does not display your email quota.
$ write username - Writes files or text to username.
$ !! - Repeats last command.
Process management
$ kill - Command use to terminate process.example: kill - 5555
$ nice program name - Command use to run program with lower priority.Recommended for running background processes.
or
or
BETWEEN SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND
value2;
or
CREATE UNIQUE INDEX index_name ON table_name (column_name);
or
or
or
MySQL
select * from (select distinct salary from emp order by salary desc limit 2) as
emp order by salary asc limit 1;
select min(salary) from emp where salary in (select distinct salary from emp order by salary desc limit 2)
;
X is the starting point (remember the first record is 0) and Y is the duration (how many records to
display).
MSSql Server
select top 1 salary from (select distinct top 2 salary from emp order by salary desc) as
emp order by salary asc ;
select min(salary) from emp where salary in (select distinct top 2 salary from emp order by salary desc);
Through sub-query also you can retrieve n highest salary by replace 'n' with required number.
select * from (select distinct salary from emp order by salary desc limit n) as
emp order by salary asc limit 1;
MSSql Server
select top 1 salary from (select distinct top n salary from emp order by salary desc) as
emp order by salary asc ;
select min(salary) from emp where salary in (select distinct top n salary from emp order by salary desc);
Files
ls --- lists your files
ls -l --- lists your files in 'long format', which contains lots of useful
information, e.g. the exact size of the file, who owns the file and who has the
right to look at it, and when it was last modified.
ls -a --- lists all files, including the ones whose filenames begin in a dot, which
you do not always want to see.
There are many more options, for example to list files by size, by date,
recursively etc.
more filename --- shows the first part of a file, just as much as will fit on one
screen. Just hit the space bar to see more or q to quit. You can use /pattern to
search for a pattern.
emacs filename --- is an editor that lets you create and edit a file. See
the emacs page.
mv filename1 filename2 --- moves a file (i.e. gives it a different name, or
moves it into a different directory (see below)
cp filename1 filename2 --- copies a file
rm filename --- removes a file. It is wise to use the option rm -i, which will ask
you for confirmation before actually deleting anything. You can make this your
default by making an alias in your .cshrc file.
diff filename1 filename2 --- compares files, and shows where they differ
wc filename --- tells you how many lines, words, and characters there are in a
file
chmod options filename --- lets you change the read, write, and execute
permissions on your files. The default is that only you can look at them and
change them, but you may sometimes want to change these permissions. For
example, chmod o+r filename will make the file readable for everyone,
and chmod o-r filename will make it unreadable for others again. Note that
for someone to be able to actually look at the file the directories it is in need
to be at least executable. See help protection for more details.
File Compression
o gzip filename --- compresses files, so that they take up much less space.
Usually text files compress to about half their original size, but it
depends very much on the size of the file and the nature of the
contents. There are other tools for this purpose, too (e.g. compress),
but gzip usually gives the highest compression rate. Gzip produces files
with the ending '.gz' appended to the original filename.
o gunzip filename --- uncompresses files compressed by gzip.
o gzcat filename --- lets you look at a gzipped file without actually having
to gunzip it (same as gunzip -c). You can even print it directly,
using gzcat filename | lpr
printing
o lpr filename --- print. Use the -P option to specify the printer name if
you want to use a printer other than your default printer. For example,
if you want to print double-sided, use 'lpr -Pvalkyr-d', or if you're at
CSLI, you may want to use 'lpr -Pcord115-d'. See 'help printers' for more
information about printers and their locations.
o lpq --- check out the printer queue, e.g. to get the number needed for
removal, or to see how many other files will be printed before yours will
come out
o lprm jobnumber --- remove something from the printer queue. You can
find the job number by using lpq. Theoretically you also have to specify
a printer name, but this isn't necessary as long as you use your default
printer in the department.
o genscript --- converts plain text files into postscript for printing, and
gives you some options for formatting. Consider making an alias
like alias ecop 'genscript -2 -r \!* | lpr -h -Pvalkyr' to print two pages on
one piece of paper.
o dvips filename --- print .dvi files (i.e. files produced by LaTeX). You can
use dviselect to print only selected pages. See the LaTeX page for more
information about how to save paper when printing drafts.
Directories
Directories, like folders on a Macintosh, are used to group files together in a
hierarchical structure.
Finding things
ff --- find files anywhere on the system. This can be extremely useful if you've
forgotten in which directory you put a file, but do remember the name. In
fact, if you use ff -p you don't even need the full name, just the beginning. This
can also be useful for finding other things on the system, e.g. documentation.
grep string filename(s) --- looks for the string in the files. This can be useful a
lot of purposes, e.g. finding the right file among many, figuring out which is
the right version of something, and even doing serious corpus work. grep
comes in several varieties (grep, egrep, and fgrep) and has a lot of very
flexible options. Check out the man pages if this sounds good to you.
Miscellaneous tools
webster word --- looks up the word in an electronic version of Webster's
dictionary and returns the definition(s)
date --- shows the current date and time.
cal --- shows a calendar of the current month. Use e.g., 'cal 10 1995' to get
that for October 95, or 'cal 1995' to get the whole year.
You can find out more about these commands by looking up their manpages:
man commandname --- shows you the manual page for the command
For further ways of obtaining help, look at the pages with electronic sources of
information and non-electronic sources.
More UNIX commands
ls command is one of the most frequently used command in Linux. I believe ls command is the first command you may use when
We use ls command daily basis and frequently even though we may not aware and never use all the ls option available. In this
article, we’ll be discussing basic ls command where we have tried to cover as much parameters as possible.
Linux ls Command
ls with no option list files and directories in bare format where we won’t be able to view details like file types, size, modified date
Here, ls -l (-l is character not one) shows file or directory, size, modified date and time, file or folder name and owner of file and
its permission.
# ls -l
total 176
-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
-rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documents
drwxr-xr-x. 4 root root 4096 Aug 16 02:55 Downloads
-rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php
-rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html
-rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
-rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Public
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templates
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videos
Using -F option with ls command, will add the ‘/’ Character at the end each directory.
# ls -F
0001.pcap Desktop/ Downloads/ index.html install.log.syslog Pictures/ Templates/
anaconda-ks.cfg Documents/ fbcmd_update.php install.log Music/ Public/ Videos/
The following command with ls -r option display files and directories in reverse order.
# ls -r
Videos Public Music install.log fbcmd_update.php Documents anaconda-ks.cfg
Templates Pictures install.log.syslog index.html Downloads Desktop 0001.pcap
7. Recursively list Sub-Directories
ls -R option will list very long listing directory trees. See an example of output of the command.
# ls -R
total 1384
-rw-------. 1 root root 33408 Aug 8 17:25 anaconda.log
-rw-------. 1 root root 30508 Aug 8 17:25 anaconda.program.log
./httpd:
total 132
-rw-r--r-- 1 root root 0 Aug 19 03:14 access_log
-rw-r--r--. 1 root root 61916 Aug 10 17:55 access_log-20120812
./lighttpd:
total 68
-rw-r--r-- 1 lighttpd lighttpd 7858 Aug 21 15:26 access.log
-rw-r--r--. 1 lighttpd lighttpd 37531 Aug 17 18:21 access.log-20120819
./nginx:
total 12
-rw-r--r--. 1 root root 0 Aug 12 03:17 access.log
-rw-r--r--. 1 root root 390 Aug 12 03:17 access.log-20120812.gz
With combination of -ltr will shows latest modification file or directory date as last.
# ls -ltr
total 176
-rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog
-rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
-rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videos
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templates
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Public
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documents
-rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html
-rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php
drwxr-xr-x. 4 root root 4096 Aug 16 02:55 Downloads
-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
With combination of -lS displays file size in order, will display big in size first.
# ls -lS
total 176
-rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
-rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html
-rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php
-rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documents
drwxr-xr-x. 4 root root 4096 Aug 16 02:55 Downloads
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Public
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templates
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videos
-rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
10. Display Inode number of File or Directory
We can see some number printed before file / directory name. With -i options list file / directory with inode number.
# ls -i
20112 0001.pcap 23610 Documents 23793 index.html 23611 Music 23597 Templates
23564 anaconda-ks.cfg 23595 Downloads 22 install.log 23612 Pictures 23613 Videos
23594 Desktop 23585 fbcmd_update.php 35 install.log.syslog 23601 Public
# ls --version
ls (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Richard M. Stallman and David MacKenzie.
# ls --help
Usage: ls [OPTION]... [FILE]...
With ls -l command list files under directory /tmp. Wherein with -ld parameters displays information of /tmpdirectory.
# ls -l /tmp
total 408
drwx------. 2 narad narad 4096 Aug 2 02:00 CRX_75DAF8CB7768
-r--------. 1 root root 384683 Aug 4 12:28 htop-1.0.1.tar.gz
drwx------. 2 root root 4096 Aug 4 11:20 keyring-6Mfjnk
drwx------. 2 root root 4096 Aug 16 01:33 keyring-pioZJr
drwx------. 2 gdm gdm 4096 Aug 21 11:26 orbit-gdm
drwx------. 2 root root 4096 Aug 19 08:41 pulse-gl6o4ZdxQVrX
drwx------. 2 narad narad 4096 Aug 4 08:16 pulse-UDH76ExwUVoU
drwx------. 2 gdm gdm 4096 Aug 21 11:26 pulse-wJtcweUCtvhn
-rw-------. 1 root root 300 Aug 16 03:34 yum_save_tx-2012-08-16-03-34LJTAa1.yumtx
# ls -ld /tmp/
drwxrwxrwt. 13 root root 4096 Aug 21 12:48 /tmp/
To display UID and GID of files and directories. use option -n with ls command.
# ls -n
total 36
drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Downloads
drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Music
drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Pictures
-rw-rw-r--. 1 500 500 12 Aug 21 13:06 tmp.txt
drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Videos
15. ls command and its Aliases
We have made alias for ls command, when we execute ls command it’ll take -l option by default and display long listing as
mentioned earlier.
# alias ls="ls -l"
Note: We can see number of alias available in your system with below alias command and same can be unalias as shown below
example.
# alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
# unalias ls
In our next article we’ll cover up more or advanced ls command with their examples. Also I suggest you to go through
some interview questions on ls command and also if we’ve missed anything in the list, please update us via comment section.
SHARE
So, let’s see how we can use the ‘wc‘ command with their few available arguments and examples in this article. We have used
the ‘tecmint.txt‘ file for testing the commands. Let’s find out the output of the file using cat command as shown below.
Red Hat
CentOS
Fedora
Debian
Scientific Linux
OpenSuse
Ubuntu
Xubuntu
Linux Mint
Pearl Linux
Slackware
Mandriva
below are 12 (number of lines), 16 (number of words) and 112 (number of bytes) of the file.
12 16 112 tecmint.txt
command will display the count of newlines in a file. In the output the first filed assigned as count and second field is the name of
file.
12 tecmint.txt
in a file.
16 tecmint.txt
112 tecmint.txt
112 tecmint.txt
16 tecmint.txt
Print newline, word, and byte counts for each FILE, and a total line if
SHARE
This website uses cookies. By using this website you accept the use of cookies. Learn more
Got it!
RapidTables
ls -r -R command in Linux
ls -r option flag lists files/directories in reverse order.
ls -R option flag lists directory tree recursively.
ls -r
ls -R
ls -r
ls -r option flag lists files/directories in reverse order.
Syntax
$ ls -r [options] [file|dir]
Examples
Default list:
$ ls
Desktop Downloads Pictures Templates Videos
Documents Music Public todo.txt
$
ls -R
ls -R option flag lists directory tree recursively.
Syntax
$ ls -R [options] [file|dir]
Examples
Recursive listing:
$ ls -R
.:
Desktop Downloads Pictures Templates Videos
Documents Music Public todo.txt
./Desktop:
examples.desktop ubiquity-gtkui.desktop
./Documents:
./Downloads:
./Music:
./Pictures:
ls-a.png ls-full-path.png ls-l.png ls-s.png
./Public:
./Templates:
./Videos:
$
./Desktop:
total 12
-rwxr-xr-x 1 user user 203 2011-08-17 16:52 examples.desktop
-rwxr-xr-x 1 user user 7672 2011-08-17 16:19 ubiquity-gtkui.desktop
./Documents:
total 0
./Downloads:
total 0
./Music:
total 0
./Pictures:
total 556
-rw-r--r-- 1 user user 94114 2011-08-17 18:13 ls-a.png
-rw-r--r-- 1 user user 254129 2011-08-17 18:14 ls-full-path.png
-rw-r--r-- 1 user user 102761 2011-08-17 18:13 ls-l.png
-rw-r--r-- 1 user user 77374 2011-08-17 18:12 ls-s.png
./Public:
total 0
./Templates:
total 0
./Videos:
total 0
$
See also
ls command
ls -a
ls -l
ls -s
ls -S
ls -t
ls full path
LS COMMAND
ls -a
ls -l
ls -r
ls -R
ls -s
ls -S
ls -t
ls full path
RAPID TABLES
Recommend Site
Send Feedback
About
1. head Command
The head command reads the first ten lines of a any given file name. The basic syntax of head
command is:
For example, the following command will display the first ten lines of the file named
‘/etc/passwd‘.
# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
If more than one file is given, head will show the first ten lines of each file separately. For
example, the following command will show ten lines of each file.
If it is desired to retrieve more number of lines than the default ten, then ‘-n‘ option is used along
with an integer telling the number of lines to be retrieved. For example, the following command
will display first 5 lines from the file ‘/var/log/yum.log‘ file.
# head -n5 /var/log/yum.log
Jan 10 00:06:49 Updated: openssl-1.0.1e-16.el6_5.4.i686
Jan 10 00:06:56 Updated: openssl-devel-1.0.1e-16.el6_5.4.i686
Jan 10 00:11:42 Installed: perl-Net-SSLeay-1.35-9.el6.i686
Jan 13 22:13:31 Installed: python-configobj-4.6.0-3.el6.noarch
Jan 13 22:13:36 Installed: terminator-0.95-3.el6.rf.noarch
In fact, there is no need to use ‘-n‘ option. Just the hyphen and specify the integer without spaces
to get the same result as the above command.
# head -5 /var/log/yum.log
Jan 10 00:06:49 Updated: openssl-1.0.1e-16.el6_5.4.i686
Jan 10 00:06:56 Updated: openssl-devel-1.0.1e-16.el6_5.4.i686
Jan 10 00:11:42 Installed: perl-Net-SSLeay-1.35-9.el6.i686
Jan 13 22:13:31 Installed: python-configobj-4.6.0-3.el6.noarch
Jan 13 22:13:36 Installed: terminator-0.95-3.el6.rf.noarch
The head command can also display any desired number of bytes using ‘-c‘ option followed by
the number of bytes to be displayed. For example, the following command will display the
first 45 bytes of given file.
# head -c45 /var/log/yum.log
Jan 10 00:06:49 Updated: openssl-1.0.1e-16.el
2. tail Command
The tail command allows you to display last ten lines of any text file. Similar to the head
command above, tail command also support options ‘n‘ number of lines and ‘n‘ number of
characters.
The basic syntax of tail command is:
For example, the following command will print the last ten lines of a file called ‘access.log‘.
# tail access.log
1390288226.042 0 172.16.18.71 TCP_DENIED/407 1771 GET http://download.newnext.me/spark.bin? - NONE/- text/html
1390288226.198 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
1390288226.210 1182 172.16.20.44 TCP_MISS/200 70872 GET http://mahavat.gov.in/Mahavat/index.jsp pg
DIRECT/61.16.223.197 text/html
1390288226.284 70 172.16.20.44 TCP_MISS/304 269 GET http://mahavat.gov.in/Mahavat/i/i-19.gif pg
DIRECT/61.16.223.197 -
1390288226.362 570 172.16.176.139 TCP_MISS/200 694 GET http://p4-gayr4vyqxh7oa-3ekrqzjikvrczq44-if-v6exp3-
v4.metric.gstatic.com/v6exp3/redir.html pg
1390288226.402 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
1390288226.437 145 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html
1390288226.445 0 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html
1390288226.605 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
1390288226.808 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
If more than one file is provided, tail will print the last ten lines of each file as shown below.
Similarly, you can also print the last few lines using the ‘-n‘ option as shown below.
# tail -5 access.log
1390288226.402 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
1390288226.437 145 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html
1390288226.445 0 172.16.18.53 TCP_DENIED/407 1723 OPTIONS http://172.16.25.252/ - NONE/- text/html
1390288226.605 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
1390288226.808 0 172.16.16.55 TCP_DENIED/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE/- text/html
You can also print the number of characters using ‘-c’ argument as shown below.
# tail -c5 access.log
ymantec.com:443 - NONE/- text/html
3. cat Command
The ‘cat‘ command is most widely used, universal tool. It copies standard input to standard
output. The command supports scrolling, if text file doesn’t fit the current screen.
The basic syntax of cat command is:
The most frequent use of cat is to read the contents of files. All that is required to open a file for
reading is to type cat followed by a space and the file name.
# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
…
# cat 1 2 3 4 > 5
# cat 5
Hi Tecmint-Team
Keep connected
Share your thought
connect us tecmint.com@gmail.com
It can be also used to create files as well. It is achieved by executing cat followed by the output
redirection operator and the file name to be created.
We can have custom end maker for ‘cat’ command. Here it is implemented.
Never underestimate the power of ‘cat’ command and can be useful for copying files.
# cat avi.txt
# cat avi1.txt
Now what’s the opposite of cat? Yeah it’s ‘tac‘. ‘tac‘ is a command under Linux. It is better to
show an example of ‘tac’ than to talk anything about it.
Create a text file with the names of all the month, such that one word appears on a line.
# cat month
January
February
March
April
May
June
July
August
September
October
November
December
# tac month
December
November
October
September
August
July
June
May
April
March
February
January
For more examples of cat command usage, refer to the 13 cat Command Usage
That’s all for now. I’ll be here again with another Interesting Article, worth Knowing. Till then
stay tuned and connected to Tecmint. Don’t forget to provide us with your valuable feedback in
our comment section.
SHARE
8. How to replace the n-th line in a file with a new line in Unix?
sed -i'' '10 i new inserted line' filename # i stands for insert
echo $?
11. How will you find which operating system your system is running on in UNIX?
uname -a
The 'history' command can be used to get the list of commands that we are executed.
By default, the head command displays the first 10 lines from a file. If we change the option of head, then we can display as many lines as we want.
The d option here deletes the lines from 21 to the end of the file
The tail command can be used to display the last lines from a file.
tail -1 filename
Recommended Reading:
p
s
As seen above, the characters a, p, s are the second character from each line of the test.txt file.
cat
cp
ls
The following specifies only the start position before the ‘-‘. This example extracts from 3rd character to end of each line from test.txt file.
The following specifies only the end position after the ‘-‘. This example extracts 8 characters from the beginning of each line from test.txt file.
cat comm
cp comma
ls comma
The entire line would get printed when you don’t specify a number before or after the ‘-‘ as shown below.
root
daemon
bin
sys
sync
games
bala
root:/root
bala:/home/bala
To display the range of fields specify start field and end field as shown below. In this example, we are selecting field 1 through 4, 6 and 7