EzDevInfo.com

unix interview questions

Top unix frequently asked interview questions

How do I remove the passphrase for the SSH key without having to create a new key? [closed]

I set a passphrase when creating a new SSH key on my laptop. But, as I realise now, this is quite painful when you are trying to commit (Git and SVN) to a remote location over SSH many times in an hour.

One way I can think of is, delete my SSH keys and create new. Is there a way to remove the passphrase, while still keeping the same keys?


Source: (StackOverflow)

Using getopts in bash shell script to get long and short command line options

I wish to have long and short forms of command line options invoked using my shell script. I know that getopts can be used, but like in Perl, I have not been able to do the same with shell.

Any ideas on how this can be done, so that i can use options like:

./shell.sh --copyfile abc.pl /tmp/
./shell.sh -c abc.pl /tmp/

In the above, both the commands mean the same thing to my shell, but using, getopts, I have not been able to implement these?


Source: (StackOverflow)

Advertisements

Grep, but only certain file extensions

I am working on writing some scripts to Grep certain directories, but these directories contain all sorts of file types.

I want to grep just .h and .cpp for now, but maybe a few others in the future.

So far I have:

{ grep -r -i CP_Image ~/path1/;

grep -r -i CP_Image ~/path2/;

grep -r -i CP_Image ~/path3/;

grep -r -i CP_Image ~/path4/;

grep -r -i CP_Image ~/path5/;} 

| mailx -s GREP email@domain.com

Can anyone show me how I would now add just specific file extensions?


Source: (StackOverflow)

Sed command find and replace in file and overwrite file doesnt work, it empties the file

I would like to run a find and replace on a html file through the command line.

my command looks something like this:

sed -e s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g index.html > index.html

When I run this and look at the file afterward, it is empty. (it deleted the contents of my file)

when i run this after restoring the file again:

sed -e s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g index.html

the stdout is the contents of the file, and the find and replace has been executed.

Why is this happening?


Source: (StackOverflow)

Check if a directory exists in a shell script

What command can be used to check if a directory exists or not, within a shell script?


Source: (StackOverflow)

How can I pretty-print JSON?

Is there a (unix) shell script to format JSON in human-readable form?

Basically, I want it to transform the following:

{ "foo": "lorem", "bar": "ipsum" }

... into something like this:

{
    "foo": "lorem",
    "bar": "ipsum"
}

Source: (StackOverflow)

What can I use to profile C++ code in Linux? [closed]

I have a C++ application I'm in the process of optimizing. What tool can I use to pinpoint my slow code?


Source: (StackOverflow)

How can I concatenate string variables in Bash?

In PHP I would add strings together like this:

$foo = "Hello";
$foo .= " World";

So $foo would be "Hello World"

How would I do that in Bash?


Source: (StackOverflow)

In the shell, what does " 2>&1 " mean?

In a Unix shell, if I want to combine stderr and stdout into the stdout stream for further manipulation, I can append the following on the end of my command:

2>&1

So, if I want to use "head" on the output from g++, I can do something like this:

g++ lots_of_errors 2>&1 | head

so I can see only the first few errors.

I always have trouble remembering this, and I constantly have to go look it up, and it is mainly because I don't fully understand the syntax of this particular trick. Can someone break this up and explain character by character what "2>&1" means?


Source: (StackOverflow)

What do 'real', 'user' and 'sys' mean in the output of time(1)?

$ time foo
real        0m0.003s
user        0m0.000s
sys         0m0.004s

Which of these three is meaningful when benchmarking my app?


Source: (StackOverflow)

How do I grep recursively?

How do I recursively grep all directories and subdirectories?

find . | xargs grep "texthere" *

Source: (StackOverflow)

NPM throws error without sudo

I just installed node and npm through the package on nodejs.org and whenever I try to search or install something with npm it throws the following error, unless I sudo the command. I have a feeling this is a permissions issue? I am already the admin.

npm ERR! Error: EACCES, open '/Users/chietala/.npm/-/all/.cache.json'
npm ERR!  { [Error: EACCES, open '/Users/chietala/.npm/-/all/.cache.json']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/Users/chietala/.npm/-/all/.cache.json' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Darwin 12.2.0
npm ERR! command "node" "/usr/local/bin/npm" "search" "bower"
npm ERR! cwd /Users/chietala
npm ERR! node -v v0.10.4
npm ERR! npm -v 1.2.18
npm ERR! path /Users/chietala/.npm/-/all/.cache.json
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, open '/Users/chietala/.npm/-/all/.cache.json'
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/chietala/npm-debug.log
npm ERR! not ok code 0

Source: (StackOverflow)

How can I replace a newline (\n) using sed?

How can I replace a newline (\n) using sed?

I unsuccesfully tried:

sed 's#\n# #g' file
sed 's#^$# #g' file

How to fix it?


Source: (StackOverflow)

How do I list all cron jobs for all users?

Is there a command or an existing script that will let me view all of a *NIX system's scheduled cron jobs at once? I'd like it to include all of the user crontabs, as well as /etc/crontab, and whatever's in /etc/cron.d. It would also be nice to see the specific commands run by run-parts in /etc/crontab.

Ideally, I'd like the output in a nice column form and ordered in some meaningful way.

I could then merge these listings from multiple servers to view the overall "schedule of events."

I was about to write such a script myself, but if someone's already gone to the trouble...


Source: (StackOverflow)

What are the dark corners of Vim your mom never told you about? [closed]

There are a plethora of questions where people talk about common tricks, notably "Vim+ctags tips and tricks".

However, I don't refer to commonly used shortcuts that someone new to Vim would find cool. I am talking about a seasoned Unix user (be they a developer, administrator, both, etc.), who thinks they know something 99% of us never heard or dreamed about. Something that not only makes their work easier, but also is COOL and hackish. After all, Vim resides in the most dark-corner-rich OS in the world, thus it should have intricacies that only a few privileged know about and want to share with us.


Source: (StackOverflow)