The command (above) will remove any duplicate rows based on the FIRST column of data in an un-sorted file.
The '$1' represents a positional parameter. You can change both instances of '$1' in the command to remove duplicates based on a different column, for instance, the third:
awk '{ if ($3 in stored_lines) x=1; else print; stored_lines[$3]=1 }' infile.txt > outfile.txt
Or you can change it to '$0' to base the removal on the whole row:
awk '{ if ($0 in stored_lines) x=1; else print; stored_lines[$0]=1 }' infile.txt > outfile.txt
** Note: I wouldn't use this on a MASSIVE file, unless you're RAM-rich ;) **
buf myfile.txt
This is useful when you are making small but frequent changes to a file. It keeps things organised and clear for another administrator to see what changed and at what time. An overview of changes can be deduced using a simple:
ls -ltr
Essentially the same as funky's alias, but will not traverse filesystems and has nicer formatting. Show Sample Output
I have come across a situation in the past where someone has unlinked a file by running an 'rm' command against it while it was still being written to by a running process.
The problem manifested itself when a 'df' command showed a filesystem at 100%, but this did not match the total value of a 'du -sk *'.
When this happens, the process continues to write to the file but you can no longer see the file on the filesystem. Stopping and starting the process will, more often than not, get rid of the unlinked file, however this is not always possible on a live server.
When you are in this situation you can use the 'lsof' command above to get the PID of the process that owns the file (in the sample output this is 23521).
Run the following command to see a sym-link to the file (marked as deleted):
cd /proc/23521/fd && ls -l
Truncate the sym-link to regain your disk space:
> /proc/23521/fd/3
I should point out that this is pretty brutal and *could* potentially destabilise your system depending on what process the file belongs to that you are truncating.
Show Sample Output
This is a handy way to circumvent the "Maximum line length of 2048 exceeded" grep error.
Once you have run the above command (or put it in your .bashrc), files can be searched using:
lgrep search-string /file/to/search
This does the same thing as many of the 'grep' based alternatives but allows a more finite control over the output. For example if you only wanted the process ID you could change the command:
ps -ef | awk '/mingetty/ && !/awk/ {print $2}'
If you wanted to kill the returned PID's:
ps -ef | awk '/mingetty/ && !/awk/ {print $2}' | xargs -i kill {}
Show Sample Output
This command will copy files and directories from a remote machine to the local one.
Ensure you are in the local directory you want to populate with the remote files before running the command.
To copy a directory and it's contents, you could:
ssh user@host "(cd /path/to/a/directory ; tar cvf - ./targetdir)" | tar xvf -
This is especially useful on *nix'es that don't have 'scp' installed by default.
This command will show the 20 processes using the most CPU time (hungriest at the bottom).
You can see the 20 most memory intensive processes (hungriest at the bottom) by running:
ps aux | sort +3n | tail -20
Or, run both:
echo "CPU:" && ps aux | sort +2n | tail -20 && echo "Memory:" && ps aux | sort +3n | tail -20
This command will tell you the 20 biggest directories starting from your working directory and skips directories on other filesystems. Useful for resolving disk space issues.
This command will replace all instances of 'foo' with 'bar' in all files in the current working directory and any sub-directories.
This command will replace all instances of 'foo' with 'bar' in all files in the current working directory.
commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.
Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.
» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10
Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):
Subscribe to the feed for: