All Questions
238,720
questions
273
votes
12
answers
339k
views
The "proper" way to test if a service is running in a script
My problem:
I'm writing a bash script and in it I'd like to check if a given service is running.
I know how to do this manually, with $ service [service_name] status.
But (especially since the move ...
273
votes
3
answers
825k
views
What is the purpose of .bashrc and how does it work?
I found the .bashrc file and I want to know the purpose/function of it. Also how and when is it used?
272
votes
15
answers
412k
views
Passing named arguments to shell scripts
Is there any easy way to pass (receive) named parameters to a shell script?
For example,
my_script -p_out '/some/path' -arg_1 '5'
And inside my_script.sh receive them as:
# I believe this ...
271
votes
13
answers
665k
views
Delete the last character of a string using string manipulation in shell script
I would like to delete the last character of a string, I tried this little script :
#! /bin/sh
t="lkj"
t=${t:-2}
echo $t
but it prints "lkj", what I am doing wrong?
271
votes
6
answers
199k
views
What do the flags in /proc/cpuinfo mean?
How can I tell whether my processor has a particular feature? (64-bit instruction set, hardware-assisted virtualization, cryptographic accelerators, etc.) I know that the file /proc/cpuinfo contains ...
271
votes
9
answers
126k
views
Where did the "wheel" group get its name?
The wheel group on *nix computers typically refers to the group with some sort of root-like access. I've heard that on some *nixes it's the group of users with the right to run su, but on Linux that ...
268
votes
10
answers
323k
views
Parallelize a Bash FOR Loop
I have been trying to parallelize the following script, specifically each of the three FOR loop instances, using GNU Parallel but haven't been able to. The 4 commands contained within the FOR loop run ...
268
votes
5
answers
1.2m
views
How to enable diffie-hellman-group1-sha1 key exchange on Debian 8.0?
I am unable to ssh to a server that asks for a diffie-hellman-group1-sha1 key exchange method:
ssh 123.123.123.123
Unable to negotiate with 123.123.123.123 port 22: no matching key exchange method ...
267
votes
10
answers
68k
views
Why *not* parse `ls` (and what to do instead)?
I consistently see answers quoting this link stating definitively "Don't parse ls!" This bothers me for a couple of reasons:
It seems the information in that link has been accepted wholesale with ...
266
votes
9
answers
164k
views
Can I create a user-specific hosts file to complement /etc/hosts?
Is it possible to add a list of hosts that are only specific to a certain user? Perhaps a user-specific hosts file?
This mechanism should also complement the entries in the /etc/hosts file.
265
votes
20
answers
215k
views
How can I add a signature .png to a PDF in Linux?
I have a scanned copy of my written signature and I need to apply it to some documents in the signature block. I used to do this on Windows all the time but I now have only Linux.
Is this possible? ...
265
votes
21
answers
382k
views
How do I get the MD5 sum of a directory's contents as one sum?
The md5sum program does not provide checksums for directories. I want to get a single MD5 checksum for the entire contents of a directory, including files in sub-directories. That is, one combined ...
265
votes
11
answers
226k
views
Allow setuid on shell scripts
The setuid permission bit tells Linux to run a program with the effective user id of the owner instead of the executor:
> cat setuid-test.c
#include <stdio.h>
#include <unistd.h>
int ...
265
votes
3
answers
557k
views
Difference between cp -r and cp -a
I'm looking for the difference between cp -r and cp -a. What does "recursive" mean in terms of copying files from a folder?
264
votes
3
answers
142k
views
Why does `htop` show more process than `ps`
In ps xf
26395 pts/78 Ss 0:00 \_ bash
27016 pts/78 Sl+ 0:04 | \_ unicorn_rails master -c config/unicorn.rb
27042 pts/78 Sl+ 0:00 | ...
264
votes
6
answers
75k
views
VISUAL vs. EDITOR – what’s the difference?
I generally set both VISUAL and EDITOR environment variables to the same thing, but what's the difference? Why would I set them differently? When developing apps, why should I choose to look at VISUAL ...
263
votes
16
answers
265k
views
How to fill 90% of the free memory?
I want to do some low-resources testing and for that I need to have 90% of the free memory full.
How can I do this on a *nix system?
263
votes
4
answers
311k
views
How to tell gzip to keep original file?
I would like to compress a text file using gzip command line tool while keeping the original file. By default running the following command
gzip file.txt
results in modifying this file and renaming ...
263
votes
3
answers
421k
views
Linux "top" command: What are us, sy, ni, id, wa, hi, si and st (for CPU usage)?
When I issue top in Linux, I get a result similar to this:
One of the lines has CPU usage information represented like this:
Cpu(s): 87.3%us, 1.2%sy, 0.0%ni, 27.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0....
262
votes
12
answers
734k
views
linux: How can I view all UUIDs for all available disks on my system?
My /etc/fstab contains this:
# / was on /dev/sda1 during installation
UUID=77d8da74-a690-481a-86d5-9beab5a8e842 / ext4 errors=remount-ro 0 1
There are several other disks on ...
261
votes
12
answers
445k
views
How do I make my pc speaker beep
Using bash, how can I make the pc speaker beep?
Something like echo 'beepsound' > /dev/pcspkr would be nice.
261
votes
6
answers
203k
views
How can I delete a word backward at the command line (bash and zsh)?
How can I delete a word backward at the command line? I'm truly used to some editors deleting the last 'word' using Ctrl+Backspace, and I'd like that functionality at the command line too.
I am ...
261
votes
7
answers
744k
views
List all connected SSH sessions?
I just SSH'd into root, and then SSH'd again into root on the same machine. So I have two windows open both SSH'd into root on my remote machine.
From the shell, how can I see a list of these two ...
261
votes
10
answers
332k
views
What causes this green background in ls output?
There are two directories shown by 'ls'. Normally directories anywhere are blue on black background. But the first one is blue on green and impossible to read. Why is this? How to make it blue on ...
261
votes
9
answers
378k
views
scp without replacing existing files in the destination
How do I copy an entire directory into a directory of the same name without replacing the content in the destination directory? (instead, I would like to add to the contents of the destination folder)
259
votes
10
answers
306k
views
Getting 256 colors to work in tmux
I have 256 colors working just fine in konsole,. I thought I'd give tmux a try because, unlike screen, it seems to support vi mode. However I find that the colors of my prompt show up and this is ...
258
votes
6
answers
712k
views
Can grep return true/false or are there alternative methods
As a part of this script, I need to be able to check if the first argument given matches the first word of file. If it does, exit with an error message; if it doesn't, append the arguments to the file....
258
votes
6
answers
482k
views
What does ` (backquote/backtick) mean in commands?
I came across the following command:
sudo chown `id -u` /somedir
and I wonder: what is the meaning of the ` symbol. I noticed for instance that while the command above works well, the one below does ...
258
votes
4
answers
510k
views
What does aux mean in `ps aux`?
ps aux seems to conveniently list all processes and their status and resource usage (Linux/BSD/MacOS), however I cannot comprehend the meaning of parameter aux using man ps.
What does aux mean?
256
votes
12
answers
506k
views
How can I calculate the size of a directory?
How to know the size of a directory? Including subdirectories and files.
256
votes
3
answers
229k
views
Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1
Just looking for the difference between
2>&-
2>/dev/null
|&
&>/dev/null
>/dev/null 2>&1
and their portability with non-Bourne shells like tcsh, mksh, etc.
254
votes
14
answers
362k
views
Find the total size of certain files within a directory branch
Assume there's an image storage directory, say, ./photos/john_doe, within which there are multiple subdirectories, where many certain files reside (say, *.jpg). How can I calculate a summary size of ...
253
votes
16
answers
319k
views
Merging folders with mv?
If I use mv to move a folder called "folder" to a directory that already contains "folder" will they merge or will it be replaced?
253
votes
4
answers
96k
views
Why is FreeBSD deprecating GCC in favor of Clang/LLVM?
So I was surfing the net and stumbled upon this article. It basically states that FreeBSD, starting from Version 10 and above will deprecate GCC in favor of Clang/LLVM.
From what I have seen around ...
253
votes
5
answers
291k
views
What is the 'working directory' when cron executes a job?
I have a script that works when I run it from the command line, but when I schedule it with cron I get errors that it cannot find files or commands. My question is twofold:
When I schedule a cron ...
252
votes
14
answers
497k
views
How to recursively find the amount stored in directory?
I know you are able to see the byte size of a file when you do a long listing with ll or ls -l. But I want to know how much storage is in a directory including the files within that directory and the ...
252
votes
20
answers
341k
views
How can I get distribution name and version number in a simple shell script?
I'm working on a simple bash script that should be able to run on Ubuntu and CentOS distributions (support for Debian and Fedora/RHEL would be a plus) and I need to know the name and version of the ...
251
votes
2
answers
902k
views
Unzipping a .gz file without removing the gzipped file [duplicate]
I have a file file.gz, when I try to unzip this file by using gunzip file.gz, it unzipped the file but only contains extracted and removes the file.gz file.
How can I unzip by keeping both unzipped ...
250
votes
12
answers
397k
views
Limit memory usage for a single Linux process
I'm running pdftoppm to convert a user-provided PDF into a 300DPI image. This works great, except if the user provides an PDF with a very large page size. pdftoppm will allocate enough memory to ...
250
votes
9
answers
311k
views
How to delete directories based on `find` output?
I issue the following command to find the .svn directories:
find . -name ".svn"
That gives me the following results:
./toto/.svn
./toto/titi/.svn
./toto/tata/.svn
How could I process all these ...
250
votes
11
answers
160k
views
Terminal prompt not wrapping correctly
I have an issue where if I type in very long commands in bash the terminal will not render what I'm typing correctly. I'd expect that if I had a command like the following:
username@someserver ~/...
250
votes
5
answers
441k
views
How to rsync only new files
I am trying to set up rsync to synchronize my main web server to the remote server by adding newly generated file to the latter.
Here is the command that I use:
rsync -avh --update -e "ssh -i /path/...
249
votes
4
answers
192k
views
Combined `mkdir` and `cd`? [duplicate]
is there any way (what is the easiest way in bash) to combine the following:
mkdir foo
cd foo
The manpage for mkdir does not describe anything like that, maybe there is a fancy version of mkdir? I ...
249
votes
5
answers
115k
views
What does env x='() { :;}; command' bash do and why is it insecure?
There is apparently a vulnerability (CVE-2014-6271) in bash: Bash specially crafted environment variables code injection attack
I am trying to figure out what is happening, but I'm not entirely sure ...
249
votes
6
answers
451k
views
How to extract specific file(s) from tar.gz
How can we extract specific files from a large tar.gz file? I found the process of extracting files from a tar in this question but, when I tried the mentioned command there, I got the error:
$ tar --...
248
votes
9
answers
207k
views
What does etc stand for?
What does the "etc" folder in the root directory stand for? I think knowing this will help me remember where certain files are located.
Update: Might be useful for others, the folder is used for "...
247
votes
11
answers
341k
views
How can I edit multiple files in Vim?
I know I can open multiple files with vim by doing something like vim 2011-12*.log, but how can I switch between files and close the files one at a time?
Also, how can I tell the file name of the ...
247
votes
4
answers
384k
views
How to export a GPG private key and public key to a file
I have generated keys using GPG, by executing the following command
gpg --gen-key
Now I need to export the key pair to a file;
i.e., private and public keys to private.pgp and public.pgp, ...
246
votes
3
answers
58k
views
Why is 'ls' suddenly wrapping items with spaces in single quotes?
I just noticed that on one of my machines (running Debian Sid) whenever I type ls any file name with spaces has single quotes surrounding it.
I immediately checked my aliases, only to find them intact....
245
votes
8
answers
342k
views
What's the difference between /sbin/nologin and /bin/false
Technically, unless pam is set up to check your shell with pam_shells neither of these can actually prevent your login, if you're not on the shell. On my system they are even different sizes, so I ...