Most powerful examples of Unix commands or scripts

2019-03-07 10:23发布

There are many things that all programmers should know, but I am particularly interested in the Unix/Linux commands that we should all know. For accomplishing tasks that we may come up against at some point such as refactoring, reporting, network updates etc.

The reason I am curious is because having previously worked as a software tester at a software company while I am studying my degree, I noticed that all of developers (who were developing Windows software) had 2 computers.

To their left was their Windows XP development machine, and to the right was a Linux box. I think it was Ubuntu. Anyway they told me that they used it because it provided powerful unix operations that Windows couldn't do in their development process.

This makes me curious to know, as a software engineer what do you believe are some of the most powerful scripts/commands/uses that you can perform on a Unix/Linux operating system that every programmer should know for solving real world tasks that may not necessarily relate to writing code?

We all know what sed, awk and grep do. I am interested in some actual Unix/Linux scripting pieces that have solved a difficult problem for you, so that other programmers may benefit. Please provide your story and source.

I am sure there are numerous examples like this that people keep in their 'Scripts' folder.

Update: People seem to be misinterpreting the question. I am not asking for the names of individual unix commands, rather UNIX code snippets that have solved a problem for you.

Best answers from the Community


Traverse a directory tree and print out paths to any files that match a regular expression:

find . -exec grep -l -e 'myregex' {} \; >> outfile.txt 

Invoke the default editor(Nano/ViM)

(works on most Unix systems including Mac OS X) Default editor is whatever your "EDITOR" environment variable is set to. ie: export EDITOR=/usr/bin/pico which is located at ~/.profile under Mac OS X.

Ctrl+x Ctrl+e

List all running network connections (including which app they belong to)

lsof -i -nP

Clear the Terminal's search history (Another of my favourites)

history -c

25条回答
我欲成王,谁敢阻挡
2楼-- · 2019-03-07 11:02

I use this so much I am actually ashamed of myself. Remove spaces from all filenames and replace them with an underscore:

[removespaces.sh]

#!/bin/bash
find .  -type f -name "* *" | while read file
do
   mv "$file" "${file// /_}"
done
查看更多
迷人小祖宗
3楼-- · 2019-03-07 11:04

Sort of an aside, but you can get powershell on windows. Its really powerful and can do a lot of the *nix type stuff. One cool difference is that you work with .net objects instead of text which can be useful if you're using the pipeline for filtering etc.

Alternatively, if you don't need the .NET integration, install Cygwin on the Windows box. (And add its directory to the Windows PATH.)

查看更多
混吃等死
4楼-- · 2019-03-07 11:05

If you make a typo in a long command, you can rerun the command with a substitution (in bash):

mkdir ~/aewseomeDirectory

you can see that "awesome" is mispelled, you can type the following to re run the command with the typo corrected

^aew^awe

it then outputs what it substituted (mkdir ~/aweseomeDirectory) and runs the command. (don't forget to undo the damage you did with the incorrect command!)

查看更多
孤傲高冷的网名
5楼-- · 2019-03-07 11:06

I find commandlinefu.com to be an excellent resource for various shell scripting recipes.

Examples

Common

# Run the last command as root
sudo !!

# Rapidly invoke an editor to write a long, complex, or tricky command
ctrl-x ctrl-e

# Execute a command at a given time
echo "ls -l" | at midnight

Esoteric

# output your microphone to a remote computer's speaker
dd if=/dev/dsp | ssh -c arcfour -C username@host dd of=/dev/dsp
查看更多
Juvenile、少年°
6楼-- · 2019-03-07 11:06

How to exit VI

:wq

Saves the file and ends the misery.

Alternative of ":wq" is ":x" to save and close the vi editor.

查看更多
爷、活的狠高调
7楼-- · 2019-03-07 11:08

Finding PIDs without the grep itself showing up

export CUPSPID=`ps -ef | grep cups | grep -v grep | awk '{print $2;}'`
查看更多
登录 后发表回答