cat
concatenate files and print on the standard output
see also :
tac
Synopsis
cat
[OPTION]... [FILE]...
add an example, a script, a trick and tips
examples
cat f - g
Output f’s contents, then standard input, then g’s contents.
cat
Copy standard input to standard output.
What does cat $1 mean?
$1 is the variable that contains the value of the first argument when calling a script.
Here is a practical example :
$ echo 'cat $1' > test_script #put the content 'cat $1' in the file test_script
$ chmod u+x ./test_script #make the script executable by the user, aka you
$ echo 'asdfaab asfa' > foo #add the line 'asdfaab asfa' in the file foo
$ ./test_script foo bar beta # call the script test_script with three arguments: foo, bar and beta
When calling the script, the first argument will be foo. Thus, the script will execute the command 'cat foo', meaning : outputing the file foo .
example added by LeBerger
source
Why is cat not changing the access time?
http://en.wikipedia.org/wiki/Stat_(system_call)
Criticism of atime
Writing to a file changes its mtime and ctime, while reading a
file changes its atime. As a result, on a POSIX-compliant system,
reading a file causes a write, which has been criticized. This
behaviour can usually be disabled by adding a mount option in
/etc/fstab.
However, turning off atime updating breaks POSIX compliance, and
some applications, notably the mutt mail reader (in some
configurations), and some file usage watching utilities, notably
tmpwatch. In the worst case, not updating atime can cause some
backup programs to fail to back up a file.
Linux kernel developer Ingo Molnár called atime "perhaps the most
stupid Unix design idea of all times," adding: "[T]hink about
this a bit: 'For every file that is read from the disk, lets do a
... write to the disk! And, for every file that is already cached
and which we read from the cache ... do a write to the disk!'" He
further emphasized the performance impact thus:
Atime updates are by far the biggest IO performance deficiency
that Linux has today. Getting rid of atime updates would give us
more everyday Linux performance than all the pagecache speedups
of the past 10 years, combined.
how to know if noatime or relatime is default mount option in
kernel?
man mount
....
relatime
Update inode access times relative to modify or change time.
Access time is only updated if the previous access time was ear?
lier than the current modify or change time. (Similar to noat?
ime, but doesn't break mutt or other applications that need to
know if a file has been read since the last time it was modi?
fied.)
Since Linux 2.6.30, the kernel defaults to the behavior provided
by this option (unless noatime was specified), and the stricta?
time option is required to obtain traditional semantics. In
addition, since Linux 2.6.30, the file's last access time is
always updated if it is more than 1 day old.
....
Which is how that particular partition was mounted and why cat
does not update the access time as I expected.
source
Linux shellscript combine all files without for loop
If there aren't too many files:
cat * > /some/new/file
Otherwise:
find . -exec cat {} + > /some/new/file
find . -exec cat {} \; > /some/new/file
cat /proc/version
## What does it do ?
Linux version and other info
## Output
Linux version 3.2.0-4-amd64 (debian-kernel@lists.debian.org) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Debian 3.2.54-2
example added by LeBerger
source
Are there any options to let cat output with color?
No, cat has no syntax highlighting abilities. If you'd like to
view source code with syntax highlighting, pop it into vim or
your editor of choice (that has syntax highlighting). This way,
you can even page through the output if it's a long file using
Ctrl + F (forward) and Ctrl +
B (backwards).
source
Is it wasteful to call cat?
Seven. But seriously, it's hard enough to know how long a disk
will last while idling let alone under heavy load. There's no
answer other than to say it will probably wear the disk
faster.
The better argument against this is it would generally be quite
slow. Why would you need to hammer the disk like that?
If you're looking to find out when something changes, perhaps
look at inotify
which is a kernel-based file event system that can call some code
when something happens, negating the need to hammer the disk.
There are wrappers like pyinotify to make things
easier.
source
Concatenate files over FTP
Unless you've got remote desktop access to the FTP server and can
open a session thru Remote Desktop and launch a concatenation
program on the server, then the answer is no : the ftp servers I
know don't allow remote execution and don't do concatenation.
I'm afraid you'll need to re-upload the unsplit file.
I would also like to add that uploading thru multiple connections
doesn't improve the upload time, which stays always limited by
your bandwidth. For example, if your upload bandwidth is 20k,
then one connection will upload at the speed of 20k, while two
connections will upload at the speed of 2X10k=20k. Total gain is
then zero. When uploading a large file, it's important to use an
FTP client that supports resumes, so in case of disconnection the
data already uploaded is not lost and you can later restart from
where you stopped.
source
Showing the line count of a specific file
You can use this command:
wc -l <file>
This will return the total line number count in the provided
file.
source
Is there a way to reassemble files split without enumerating all of the parts explicitly?
This is what wildcards and brace expansion are for. See if
echo file.bz2.part-*
returns the filenames in the
desired order, and use cat file.bz2.part-* >
file.bz2
if it does. Otherwise, figure out some other more
complex expansion that does.
source
List only the device names of all available network interfaces
to just print the first column:
netstat -a | awk '{print $1}'
you can incorporate other rules in awk to add or remove entries
as needed.
EDIT: same goes with ifconfig (like Doug pointed out)
ifconfig | awk '{print $1}'
This is an example excluding the 'lo' interface
ifconfig | awk '{if ($1 != lo) print $1}'
source
Linux cat example
Because it opens and truncates the file before reading the data —
it being shell, the redirections are processed by shell before
even starting cat
.
source
Cat command output varies unusually in regards to * character
Remember that the command line is parsed and wildcards are
replaced BEFORE it is executed.
If you specify the username, then the sudo ... command can see
the file because it's accessing it as root.
When you type '*', your USER shell expands that to the paths
it can see, well... the paths which allow your
current user to search the subdirectories.
You might try enclosing the path string in SINGLE quotes to
prevent expansion, I'm not sure that'll work since most unix
programs expect the shell to expand wildcards and thus don't
perform the globbing action themselves.
EDIT: A thought about how to accomplish your goal of reading the
authorized keys files for all the users:
sudo find /home -name "authorized_keys" -exec cat "{}" \; > all_the_data
Maybe crude, but it'll work since you're executing the file
search as root, not your user.
I suppose another way:
sudo 'find /home -name "authorized_keys" -print0 | xargs -0 cat' > all_the_data
That one's nicer on the process count, since it accumulates all
the filenames and then 'cat's them... but I like the first one,
no special quoting required.
The quotes (single or otherwise) around the entire command are
required because of the pipe command. both the find and xargs
command MUST be executed as root. Yeah, more complicated than the
first one.
source
Can cat be used to clone a partition?
In principle, you could use either. There are few important
differences, but none that apply here.
-
When you use >
redirection, the target file
is opened, and truncated. Only then it is written to. However
this does not apply to block devices — they have a fixed
size, so “truncation” doesn't do anything to them.
-
With cat
you can not easily tell it to only copy
the first n bytes or skip/seek.
This is what dd
is useful for.
-
cat
does not let you specify a block size. This
won't matter today when block sizes are masked by the file
systems being used, but it used to make a difference where
devices would be read from with specific block sizes (tapes).
-
For hard disks,
cat
may be slightly faster (better even than
dd
with a well-chosen block size, let alone the
default which slows things down).
source
How to recursively find a .doc file that contains a specific word?
Use find
for recursive searches:
find -name '*.doc' -exec catdoc {} + | grep "specificword"
This will also output the file name:
find -name '*.doc' | while read -r file; do
catdoc "$file" | grep -H --label="$file" "specificword"
done
(Normally I would use find ... -print0 | while read
-rd "" file
, but there's maybe a .0001% chance that it
would be necessary, so I stopped caring.)
source
Utility to cat/gunzip a list of files
less
does that.
less /var/log/messages*
If you pipe the output of less
to another program,
less
will behave like cat
or
gunzip -c
and not page the output.
description
Concatenate
FILE(s), or standard input, to standard output.
-A, --show-all
equivalent to
-vET
-b,
--number-nonblank
number nonempty output lines,
overrides -n
-E,
--show-ends
display $ at end of each
line
-n,
--number
number all output lines
-s,
--squeeze-blank
suppress repeated empty output
lines
-T,
--show-tabs
display TAB characters as
^I
-v,
--show-nonprinting
use ^ and M- notation,
except for LFD and TAB
--help
display this help and exit
--version
output version information and
exit
With no FILE,
or when FILE is -, read standard input.
copyright
Copyright © 2012 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.
reporting bugs
Report cat bugs to bug-coreutils[:at:]gnu[:dot:]org
GNU coreutils home page:
<http://www.gnu.org/software/coreutils/>
General help using GNU software:
<http://www.gnu.org/gethelp/>
Report cat translation bugs to
<http://translationproject.org/team/>
see also
tac
The full
documentation for cat is maintained as a Texinfo
manual. If the info and cat programs are
properly installed at your site, the command
info
coreutils 'cat invocation'
should give you
access to the complete manual.
author
Written by
Torbjorn Granlund and Richard M. Stallman.