Hot answers tagged cat
37
The Windows equivalent in command.com, cmd, and other variants is type.
From the Wikipedia article (emphasis mine):
In computing, type is a command in various VMS. AmigaDOS, CP/M, DOS, OS/2 and Microsoft Windows command line interpreters (shells) such as COMMAND.COM, cmd.exe, 4DOS/4NT and Windows PowerShell. It is used to display the contents of ...
16
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 ...
16
It may have no effect at all, depending on the size of somefile.txt - if it's small enough for the kernel to cache it in RAM, the file will only be read once from disk and subsequent iterations will retrieve it from the cache.
Even if running that command repeatedly does have an effect on your drive's lifetime, it will be due to the file being read ...
13
A GNU package, source-highlight, seems to do the trick (though isn't using cat -- as John T points out, this isn't possible with cat specifically). It's available via apt-get on Ubuntu, and requires the Boost regex library. Check your package manager to see if both are available, otherwise you can grab them from the web. The GNU page linked earlier has a ...
11
From the command shell:
copy a.txt + b.txt + c.txt output.txt
(But that follows the command shells use of control-Z as an end of file marker, so not suitable in some cases).
In PowerShell:
get-content a.txt,b.txt,c.txt | out-file output.txt
and you can control (using -Encoding parameter) the file encoding (which allows transcoding by using different ...
10
It's useless in the sense that using it like that doesn't accomplish anything the other, possibly more efficient options can't (i.e. producing proper results).
But cat is way more powerful than just cat somefile. Consult man cat or read what I wrote in this answer. But if you absolutely positively only need the contents of a single file, you might get some ...
7
The most obvious way is tail, the syntax might be slightly different depending on what OS you are using:
tail -n +70000
If you can not get tail to work, you could use sed, but it might end up slower.
sed -pe '1,69999d'
6
In every day command line use it's not really much different. You especially aren't going to notice any speed difference since the time on CPU avoided by not using cat, your CPU is just going to be idle. Even if you're looping through hundreds or thousands (or even hundreds of thousands) of items in all practicality it's not going to make much difference, ...
6
Redirecting. At the end of any command, type > filename.txt. Replace filename.txt with the file name you want to use. You can even output to different devices (dangerous) or directories (ls > /home/user/ls.txt for example)
As @Josh pointed out as well:
If you need to see the output too you can use the tee command to output to stdout as well as a ...
6
You can use find (man page) to accomplish this:
find -name "*.java" -exec cat {} \;
You can also add a -print before the -exec to print the file name before each cat operation
5
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 ...
5
A third option would be
cat 'file name with space'
where the file name may contain everything but the '.
If it does, such as file n'ame, replace every ' with '\'':
cat 'file n'\''ame'
5
cat * >/path/to/somewhere
don't do
cat * > toall.txt
because "toall.txt" is created before cat is started and you will get strange result, "cat"ing toall.txt into toall.txt.
if want cat in the current directory, you should use
cat [some_globbing] > file #or
cat * > .dotted_file
.dotted_file is not expanded by * globbing.
or for example
...
5
Heredoc usage, or "appending to EOF", is not the problem.
All redirections (including >) are applied before executing the actual command. In other words, your shell first tries to open /etc/php5/apache2/php.ini for writing using your account, then runs a completely useless sudo cat.
One way to get around this:
sudo bash -c "cat >> ...
5
What happens is that cat treats --no-recursion as its option. You can do either of the following:
cat ./--no-recursion
OR
cat -- --no-recursion
Can do the same thing with rm to delete this file (since it's probably an error of some kind).
In my first example, I prepended current directory path ("./") to the file name, so it does not start with an ...
5
I think the position being taken by some of those commenting on something being a UUOC is that if one really understands Unix and shell syntax, one would not use cat in that context. It's seen as like using poor grammar: I can write a sentence using poor grammar and still get my point across, but I also demonstrate my poor understanding of the language and ...
5
To output syntax highlighted code with something like cat, I created a ccat command by following the instructions at http://scott.sherrillmix.com/blog/programmer/syntax-highlighting-in-terminal/.
#!/bin/bash
if [ ! -t 0 ];then
file=/dev/stdin
elif [ -f $1 ];then
file=$1
else
echo "Usage: $0 code.c"
echo "or e.g. head code.c|$0"
exit 1
fi
...
5
xclip can't talk to your X server. Check that $DISPLAY is set correctly. Do other X clients work with the same $DISPLAY setting?
This:
$ echo 'hello' | xclip
works just fine for me.
edit
You get the error after su'ing to another user because that user doesn't have appropriate permissions to connect to your X server. X uses a permission checking ...
4
I took a look at the FreeBSD source code for cat(1), and the relevant source lines are:
case 'b':
bflag = nflag = 1; /* -b implies -n */
So this looks like a deliberate design decision; the interpretation of -b is that it modifies the behavior of -n, rather than -b and -n being two mutually exclusive alternatives.
4
If you were running cat on the actual ksh binary file, then you were seeing the raw machine code of the ksh program being interpreted as if it was human-readable text. Of course, it wasn't, so the characters displayed were random characters from your terminal's character set, plus control and other non-printable characters.
Some of the control characters ...
4
cat myfile.txt todo.txt > temp && mv temp todo.txt
cat con"cat"enates several files. In the original solution, the first file
was -, which means "standard input", i.e., whatever is piped into the cat command. Here you have already two named files, namely myfile.txt and todo.txt, so you can just use them as arguments for cat.
3
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 ...
3
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 ...
3
sed -i -e '1s/^/header\n/' -e '$s/$/\nfooter/' $file
Update: even shorter:
sed -i '1s/^/header\n/;$s/$/\nfooter/' $file
Update: and even shorter (but must be on many lines as 'i' and 'a' commands in 'sed' must be followed by a \<newline>):
sed -i '1i\
header
;$a\
footer' $file
2
The FTP protocol (and therefore the standard FTP Server) does not allow any server file manipulations besides overwriting them with new copies. Concatenating files uploaded on the server does not seem feasible over FTP.
Check if you have a secure shell (SSH) or at least TELNET login possible on the server to concatenate these files.
To achieve bandwidth ...
2
It's possible wget is taking time to download some of the files. Are there any wget/xargs processes in memory during the period that it appears to be hung? If so, is it the full 50 processes as you allocated with the -P50 flag to xargs, or has it somehow creeped up over that number or less than that number and no new instances are being spawned properly? ...
2
What would be really nice is a shell that supports syntax like:
< filename cmd | cmd2 cmd2arg1... | cmd3
In the meantime, I think cat filename | realcmd1... is acceptable, as it keeps the syntax standardised with initial commands that require the filename as an argument.
2
I have found similar problems from time to time: it may be a particular process or application which prevents the idle timer from kicking in.
Try quitting programs like VLC, Quicktime Player and the like.
A good test might be to restart the machine, and then see if it will allow the monitor to go to sleep. Then use ps or Activity Monitor to see which ...
2
It's true that Airport could be waking your computer, but then simply unchecking 'Wake for network access' box should fix this (see SysPref pane view below).
You may want to try a few tests like forcing your monitor to sleep by pressing SHIFT-CTRL-EJECT together. I have a login prompt screen after moving my mouse, but leaving it 30 seconds should ...
2
You could create a small wrapper script like so:
#!/bin/bash
if (( `wc -l < $1` < 20 ))
then
cat $1
else
less $1
fi
If you create that in /usr/local/bin as a file called something like less2 (you might want to use a very short name like l for easy typing) and make sure it is executable with chmod a+x /usr/local/bin/less2 you can use ...
Only top voted, non community-wiki answers of a minimum length are eligible