Hot answers tagged

87

type It works across command.com, cmd, and PowerShell (though in the latter it's an alias for Get-Content, so is cat, so you could use either). 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, ...


23

Take a look at tail, more precisecly, it's --lines=+N switch: tail --lines=+100 <file>


23

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 ...


19

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 ...


19

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 ...


18

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 ...


16

Because it opens and truncates the file before reading the data — it being shell, the redirections are processed by shell before even starting cat.


16

You can use this command: wc -l <file> This will return the total line number count in the provided file.


16

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 ...


14

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 ...


13

The > redirection happens first and opens file.txt for writing which clears any existing content.


13

Press Control-D.


12

ls *.txt | xargs cat >> all.txt might work a bit better, since it would append to all.txt instead of creating it again after each file. By the way, cat *.txt >all.txt would also work. :-)


12

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


9

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 ...


9

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'


9

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 ...


9

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, ...


8

You have to send an EOF (^D) character on the standard input to tell cat to stop.


8

Give this a try: ifconfig -a | sed 's/[ \t].*//;/^$/d' This will omit lo: ifconfig -a | sed 's/[ \t].*//;/^\(lo\|\)$/d'


8

Command substitution. ./Myscript.sh "$(cat text.txt)"


8

tail supports several files, for example: tail -q -f file1 file2


8

Well, one thing you could do is simply disable the power button altogether. Personally, I only use it to turn on my machine, and never use it once the machine is on. If this is an OK solution for you, edit /etc/acpi/events/powerbtn-acpi-support: sudo nano /etc/acpi/events/powerbtn-acpi-support That file should look something like this: event=button[ ...


7

Type Ctrl+D at the start of a new line. ^D is the "end of file" character when typed from a keyboard. Note that the ^D character itself is not entered into the file. The file ends just before the point where you type ^D.


7

The obviously easy solution would be to not use cat. Your shell isn't a text viewer. Use less which is designed for this.


7

I often use cat file | myprogram in examples. Sometimes I am being accused of Useless use of cat (http://partmaps.org/era/unix/award.html). I disagree for the following reasons: It is easy to understand what is going on. When reading a UNIX command you expect a command followed by arguments followed by redirection. It is possible to put the redirection ...


7

cat file1.txt folder1/file2.txt folder2/file3.txt > single.txt


7

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 >> ...


6

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 ...


6

Try quoting the first EOF, e.g., cat <<'EOF' >checkup.sh\n'$command' EOF This is explained in the bash(1) man page, in the section, Here Documents.



Only top voted, non community-wiki answers of a minimum length are eligible