Tagged Questions

The shell is unix's command-line interface. You can type commands in a shell interactively, or write scripts to automate tasks. Use this tag for questions applying to /bin/sh and most compatible shells (ash, bash, ksh, zsh, …).

learn more… | top users | synonyms (1)

6
votes
4answers
530 views

How to cd automatically after git clone?

I want to automatically cd to the directory created by the clone command after I git cloned something. That is, without writing anything additional just git clone $url that the shell will somehow ...
3
votes
2answers
40 views

Overriding amended time stamps

I need to make it so the files in current directory get amended with a timestamp. It works fine for the first time, but once I run it again it adds another timestamp to the file. I need a way to ...
0
votes
2answers
528 views

Bash completion throwing syntax error

THE PROBLEM: Say I'm trying to change to a directory called 'build' which is in the directory I'm currently in. I start: cd b Then I hit tab once (knowing that no other directories in the current ...
3
votes
1answer
381 views

Array Cartesian product in bash

Is there a way to generate cartesian product of arrays without using loops in bash? One can use curly brackets to do a similar thing: echo {a,b,c}+{1,2,3} a+1 a+2 a+3 b+1 b+2 b+3 c+1 c+2 c+3 but I ...
22
votes
5answers
2k views

Pipe to multiple files in the shell

I have an application which will produce a large amount of data which I do not wish to store onto the disk. The application mostly outputs data which I do not wish to use, but a set of useful ...
3
votes
2answers
289 views

List all dotfiles current directory using zsh

How can I list all dotfiles in a directory using zsh.?
3
votes
4answers
16k views

How to find and replace string without use command Sed?

As we all know, sed is greatly efficient to find and replace string, for example find 'a' and replace it to 'b': sed 's/a/b/g'. Is it possible to do this with other command or shell script instead ...
0
votes
1answer
494 views

Unable to execute command in the Linux with an user

Two scenarios here. Sudo rights are given for a particular group. a. A new user is created and is assigned to the group. b. Using this user, when we execute commands via shell script it is unable to ...
4
votes
1answer
159 views

IFS null is not the same as unset IFS?

I read a great question on here called Understanding IFS. I was surprised because the answers and comments quote POSIX which states that IFS= is not the same as unsetting IFS. If you unset IFS, ...
4
votes
3answers
137 views

Transactional UNIX shell for file system changes

It is easy to make a mistake when copying (cp), moving/renaming (mv), removing (rm/rmdir), creating dirs (mkdir), and changing file permissions (chmod). It would be nice to have a log to view before ...
4
votes
1answer
103 views

Zsh completion that is insensitive between `_` and `-`

I don't like typing underscores, and would like to type a dash instead, and have zsh autocompletion treat it like it could be either a dash or an underscore. Is this possible with built-in zsh ...
2
votes
3answers
614 views

Running tcpdump, tee and scp

I'm trying to run the following: $ tcpdump -l -X -i eth1 -n tcp port <port> | tee capture.txt | \ scp capture.txt root@10.3.3.227:/home/checker/ tcpdump -l -X -i eth1 -n tcp port ...
5
votes
4answers
121 views

Is it shell portable to run a command on the same line after variable assignment?

Is there any standard that covers the portability of running a command after variable assignment on the same line? APPLE="cider" echo hi How portable is something like that? Where will it work and ...
3
votes
1answer
1k views

How to grep top most frequent error messages in a unix logfile

If I have a file example apache log file How to extract the top most frequent error messages in a unix log file with no timestamps the key is most frequent error message should come on top of the ...
0
votes
1answer
1k views

Getting a date format with grep

I've looked at similar questions and tried most suggestions, but none of them got me anywhere, even though i have a fairly simple task I think :S. I'm fairly new to this whole thing, so I've run out ...
3
votes
1answer
664 views

Is there something like a lorem ipsum generator?

Is there something like a 'lorem ipsum generator'? I know it exists in Latex, and even in LibreOffice, but I'm looking for a terminal command. I would like to type something like loremipsum 10 ...
3
votes
2answers
202 views

“$!” is used for? [duplicate]

While going through: info coreutils 'dd invocation' I came across: dd if=/dev/zero of=/dev/null count=10MB & pid=$! What is $! used for?
2
votes
4answers
5k views

How to catch an error in a linux bash script?

I made the following script: # !/bin/bash # OUTPUT-COLORING red='\e[0;31m' green='\e[0;32m' NC='\e[0m' # No Color # FUNCTIONS # directoryExists - Does the directory exist? function directoryExists ...
16
votes
4answers
2k views

Why does while [ 0 ] go into infinite loop?

I see the same behaviour for below loop as the loop with while [ 1 ]. Why is that so? while [ 0 ]; do echo "hello" done
8
votes
6answers
800 views

How to get multiple lines out of a file by a regex?

How to get multiple lines out of a file by a regex? I often would like to get multiple lines / modify multiple lines by a regex. A example case: I am trying to read part of an XML/SGML file (they ...
1
vote
0answers
355 views

Using node.js and shelljs as interactive shell

Following the answer given at unix.stackexchange.com/questions/58011/is-there-a-javascript-shell That requires me to always execute var shell=require("/usr/local/lib/node_modules/shelljs"); It ...
3
votes
1answer
109 views

Test for link with trailing slash?

I created in /tmp/test/ mkdir somedir ln -s somedir/ somelink I want to loop through only directories: for file in */ ; do if [[ -d "$file" && ! -L "$file" ]]; then echo "$file is a ...
6
votes
2answers
9k views

How do I check if a file is a symbolic link to a directory?

I can check, if a file exists and is a symbolic link with -L for file in *; do if [[ -L "$file" ]]; then echo "$file is a symlink"; else echo "$file is not a symlink"; fi done and if it is a ...
2
votes
1answer
85 views

Why do I see myself twice when I run the who command? [duplicate]

When I run who on my ubuntu laptop I see myself listed twice. Once with pts/0 and once with pts/2. Why is that?
13
votes
4answers
279 views

pipe, { list; } only works with some programs

Need explanations from power users for such unpredictable behaviour: ps -eF | { head -n 1;grep worker; } UID PID PPID C SZ RSS PSR STIME TTY TIME CMD root 441 2 0 ...
2
votes
1answer
47 views

Piping fragments of a document through various commands

I want to send, maybe using escape sequences such as $$$, fragments of an article through various commands, possibly with modifiers. The stdin would be replaced by the corresponding stdout. (Deleting ...
12
votes
4answers
921 views

Search for a previous command with the same prefix when I press Up at a shell prompt

Coming from a FreeBSD world I wish to make the Linux terminal behave like FreeBSD one, especially the 9.1 version, basically when you type cd in the terminal and push the "up" arrow you can browse all ...
2
votes
5answers
6k views

How to append date to backup file

I need to make a backup of a file, and I would like to have a timestamp as part of the name to make it easier to differentiate. How would you inject the current date into a copy command? ...
3
votes
1answer
1k views

Find location of sourced shell script

Is it possible that a sourced shell script knows its location? I've read determining path to sourced shell script but the answers focus on bash and tcsh and fail if a POSIX shell is used. $0 is also ...
2
votes
4answers
332 views

Recursively apply a command to modify all files in a directory

I have a command that reads a file, and outputs a modified version on stdout: ./convert /path/to/file How can I recursively apply this command to all files in a directory, and overwrite the ...
1
vote
1answer
124 views

How to pass wildcards in command line [closed]

So this works: foo -a -b -c "path/file.ext" And this too if I want to pass all files from directory: foo -a -b -c path/* But if I add the quotes foo -a -b -c "path/*" It doesn't work anymore: ...
2
votes
1answer
57 views

How do I drop to a shell after exiting a command I started by scripting tmux?

I'm following this tutorial for scripting the creation of tmux windows. tmux new-window t $SESSION:0 -k -n MAIN 'cd ~/main/ && perl -Ilib myapp' sometimes I kill myapp after I've started ...
2
votes
1answer
43 views

Inner logic of chained redirections

When I type cat some_file > new_file1 > new_file2 I got output from some_file in new_file2 and empty new_file1. What is the inner logic in it? More specifically what is the stdout of file? ...
3
votes
2answers
1k views

Replace a pattern in a file with a long string

I'm writing a little shell script to generate a directory listing. To make the output easily customizable, the script just builds a HTML-table and than should replace a specific token in a template ...
-1
votes
2answers
148 views

Alternative shells (Javascript, Ruby, etc)? [duplicate]

Does anyone use a shell written in Javascript, Ruby, Clojure, etc as their everyday unix shell, instead of bash, zsh, etc? Is there any reason not to?
6
votes
3answers
864 views

How to stop Fork Bomb out of memory error - RHEL 6

I set up test VM to test the effect of fork bombs. So I edited the limits.conf as follows for root user: root hard nproc 512 Now I drop a fork bomb like so: :(){ :|:& };: After ...
1
vote
4answers
255 views

List all commands that a shell knows

What can I type at my shell (which happens to be bash) that will list all the commands that are recognized? Also, does this differ by shell? Or do all shells just have a "directory" of commands they ...
1
vote
2answers
901 views

How to find certain files and move them to a new directory in the same command?

So I was looking around and all I have found is how to do this only if the directory is already made, which is this: find source -name '*.z' -exec cp {} destination \; But how can I make a new ...
0
votes
2answers
724 views

Why alias does not work in AIX (korn shell, .profile)?

I have these files in my home directory: drwxr-xr-x 3 meuser staff 256 Oct 12 13:11 . drwxr-xr-x 102 bin bin 4096 Sep 30 12:28 .. -rw------- 1 meuser staff ...
5
votes
1answer
2k views

How to echo variables using cat into file?

I have simple script like this: #!/bin/bash BOO=bla-bla-bla cat > ./Makefile <<'EOF' Hello INCLUDES := -I/data/opt/include/ -Ideps/ -I$BOO/include EOF Yet it outputs into ...
2
votes
1answer
109 views

Why does copying from a script screw with my files?

I am at a loss here, trying to find out the reason why a shell script that is basically copying a couple of files and directories from one place to another will result in the content of two of the ...
0
votes
2answers
84 views

Eval command in unix [closed]

Can someone convert this code? I got this from the web. I am new on this and don't know how eval works. Originally, there is a parameter from a command that is either a plus (+) sign or a minus (-) ...
3
votes
2answers
1k views

Bash doesn't read .bashrc unless manually started

bash won't source .bashrc from an interactive terminal unless I manually run bash from a terminal: $ bash or manually source it: $ source ./.bashrc or running: $ st -e bash Here's some useful ...
8
votes
1answer
4k views

How to change cron shell (sh to bash)?

Is it possible to make commands in crontab run with bash instead of sh? I know you can pass commands to bash with -c, but that's annoying and I never use sh anyway.
1
vote
1answer
102 views

multiple conversion bam to bed using bedtools

I have ten .bam files (bioinformatic format) and I would like to convert it to 10 .bed files, but for this conversion I need to use special command bedtools bamTobed -i (input file) > output ...
2
votes
1answer
99 views

Why the order of wildcard results might be different when used from the terminal or from perl?

There's a folder with a bunch of files and a perl scrip that lists them using ls with a wildcard like: #!/usr/bin/perl system("ls -U -1 dir/*"); I noticed that if I run exactly the same command ...
1
vote
1answer
753 views

using find in Bash Script and checking if file is present

var= "$(find . -name 'gen*.bt2')" if [ "$var" == "" ] then echo bad else echo great fi I get errors ./script.sh: line 4: ./gen.4.bt2 ./gen.rev.1.bt2 ...
1
vote
1answer
142 views

How to safely move a file to another directory and remove duplicates

I want to move files from Downloads, Documents, Pictures, Music, et al to shared locations. This is for various reasons, including because I want to keep media in a central, shared place. I want to ...
2
votes
1answer
109 views

Any better method than this for sorting files by their creation date?

I needed to display a list of directories sorted by their creation date and I came up with this code snippet that I thought was kind of clever. Is there a more obvious way to do this that I'm missing? ...
20
votes
3answers
5k views

Find out current working directory of a running process?

What command(s) can one use to find out the current working directory (CWD) of a running process? These would be commands you could use externally from the process.