The shell is unix's command-line interface. You can type commands in a shell interactively, or write scripts to automate tasks.
6
votes
2answers
914 views
List elements with spaces in zsh
I've been studying zsh scripting for all of 2 hours at this point and I've hit a wall. I want to go through a list of files that may have spaces in them. I'm open to completely different approaches ...
6
votes
2answers
238 views
How can I use $var in a shell brace expansion of a sequence?
I want to use $var in a shell brace expansion with a range, in bash.
Simply putting {$var1..$var2} doesn't work, so I went "lateral"...
The following works, but it's a bit kludgey.
# remove the ...
5
votes
4answers
465 views
List X random files from a directory
Is there a way to list a set of say, 30 random files from a directory using standard Linux commands? (in zsh)
The top answer described here does not work for me (sort does not recognize the option ...
5
votes
4answers
2k views
Run script in a non interactive shell?
I have a cron job that is running a script. When I run the script via an interactive shell (ssh'ed to bash) it works fine. When the script runs by itself via cron it fails.
My guess is that it is ...
4
votes
3answers
2k views
Why the double quotes and backquotes in a shell script?
I'm looking at this script mysql backup script and I don't understand what is the reason for using both backticks and double quotes around command names?
379 WHICH="`which which`"
380 AWK="`${WHICH} ...
4
votes
2answers
2k views
How do I insert a space every four characters in a long line?
I've got a long line that I want to insert a space every 4 characters, on a single lone line of solid text to make it easier to read, what's the simplest way to do this? also I should be able to input ...
12
votes
4answers
725 views
How can I create an alias for a command that includes a space?
Most of my my aliases are of this form: alias p='pwd'
I want to alias git commit so that it does git commit -v
But trying to create an alias with a space gives an error:
$ alias 'git commit'='git ...
12
votes
2answers
614 views
Does redirecting output to a file apply a lock on the file?
If I have a command
$ ./script >> file.log
that gets called twice, with the second call occurring before the first one ends, what happens?
Does the first call get an exclusive lock on the ...
11
votes
4answers
6k views
How can I play a sound when script execution is ready?
I am executing every now and then some python scripts which take quite long to execute.
I execute them like this:
$ time python MyScript.py
How can I play a sound as soon as the execution of the ...
10
votes
7answers
6k views
grep and tail -f?
Is it possible to do a tail -f (or similar) on a file, and grep it at the same time? I wouldn't mind other commands just looking for that kind of behavior.
9
votes
5answers
325 views
Why do I need to quote variable for if, but not for echo?
I've read that you need double quotes for expanding variables, e.g.
if [ -n "$test" ]; then echo '$test ok'; else echo '$test null'; fi
will work as expected, while
if [ -n $test ]; then echo ...
9
votes
5answers
1k views
Creating numerous directories using mkdir
I would like to create many directories using mkdir. Each directory name will consist of a prefix (a string) and an index (an integer). Suppose that I would like the prefix to be "s" and the indices ...
9
votes
3answers
404 views
Doing two things with output from a command
I have a program texcount that outputs the number of words in my LaTeX document. I can also pipe the output of this to sed to make the newlines TeX linebreaks and write this to a file which I can then ...
8
votes
5answers
2k views
How to pipe output from one process to another but only execute if the first has output?
How can I rewrite this command to only email if there is output from the mailq | grep?
mailq | egrep 'rejected|refused' -A 5 -B 5 | mail -s 'dd' email@email
Is this even possible on one line?
8
votes
5answers
1k views
find searching in parent directories instead of subdirectories
I'm nested deep in a file tree, and I'd like to find which parent directory contains a file.
E.g. I'm in a set of nested Git repositories and want to find the .git directory controlling the files I'm ...
8
votes
1answer
3k views
touch: cannot touch `foo': No such file or directory
What could cause touch to fail with this error message?
touch: cannot touch `foo': No such file or directory
Note that an error due to incorrect permissions looks different:
touch: cannot touch ...
7
votes
3answers
132 views
+100
Why do I have to re-set env vars in tmux when I re-attach?
I mainly work on a mac and ssh/tmux attach to a Linux machine to do my work. I have ssh-agent running on the Linux machine. I have
set -g update-environment "SSH_AUTH_SOCK SSH_ASKPASS WINDOWID ...
7
votes
3answers
5k views
What does ` (backquote/backtick) mean in bash?
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 ...
6
votes
2answers
146 views
Replacing tmux with exec
I'm using tmux from a while and I'm very happy with it. Since I need it most of the time, I made it my default shell (in guake, to be precise).
This is nice and I'm perfectly fine with this.
But ...
6
votes
4answers
1k views
using parallel to process unique input files to unique output files
I have a shell scripting problem where I'm given a directory full of input files (each file containing many input lines), and I need to process them individually, redirecting each of their outputs to ...
6
votes
4answers
475 views
how can a makefile detect whether a command is available in the local machine?
I began to use org-mode for planning out my tasks in GTD-style system. Putting every org files in a directory of a Dropbox folder, I run emacs to edit / manage these files from three different local ...
6
votes
4answers
1k views
`highlight` command
Is there a command which can be used to easily highlight (with colors, bold text or inverted back-/foreground) specific strings in stdin while passing the entire string to stdout? I'm thinking of ...
5
votes
2answers
99 views
When is double-quoting necessary?
The old advice used to be to double-quote any expression involving a $VARIABLE, at least if one wanted it to be interpreted by the shell as one single item, otherwise, any spaces in the content of ...
5
votes
2answers
209 views
How to cut (select) a field from text line counting from the end?
I know how to select a field from a line using the cut command. For instance, given the following data:
a,b,c,d,e
f,g,h,i,j
k,l,m,n,o
This command:
cut -d, -f2 # returns the second field of the ...
5
votes
2answers
515 views
Giving some users only SSH and shell access with limited resource usage
I'm trying to make a user that can access my server through SSH and allow it to have a shell, limited to using only, say screen and irssi. And it shouldn't it be able to see other directories except ...
5
votes
2answers
752 views
Pasting from clipboard to vi-enabled zsh or bash shell
I'd like to be able to paste from the system clipboard (or text selection) into my "vi-like" shell prompt using the keyboard. I normally use zsh and sometimes bash. In both cases, I have the shell set ...
5
votes
1answer
254 views
I'm confused as to why “| true” in a makefile has the same effect as “|| true”
I have a makefile where I'm stopping a service before removing a file. When it couldn't stop the service, it would break on error. This is clearly unwanted so I thought I'd add || true but missed a |. ...
5
votes
1answer
704 views
Is it possible to use named colors in Zsh beyond ANSI names?
I know there are ways to use ANSI color names in Zsh (such as red), but Zsh supports 256 colors by number. I'm curious if there's any way to refer to the non-ANSI colors by a name? (Without just ...
4
votes
2answers
82 views
Extracting queries from log file that do not include banned lines
I have a log file that looks something like the following:
query1 startQuery
query1 do something
query1 do something else
query2 startQuery
query1 do something banned
query2 do something
query3 ...
4
votes
1answer
603 views
How to pipe the result of 'date' command to 'mv' command
I want to rename my file to the current timestamp.I know how to get the current timestamp("date +%s") but how can I pipe it to 'mv' command?
3
votes
1answer
635 views
How to move file based on file name expression using shell script?
I want to achieve the following using a shell script. When a script is called with a file as a parameter, it should move the file based on a portion of its name.
For example,
"Car insurance ...
3
votes
2answers
345 views
Convert a typescript file to a list of commands (history)
I wanted to record a linux session so I could use it as documentation for a "how to install" guide. I found something on the internet that suggested that the script command would be good for this, and ...
3
votes
1answer
280 views
Difference between ' and " on command line (bash)?
I used to use '' and "" interchangeably on the command line, but I recently noticed that '$HOME/some/dir' is not expanded, while "$HOME/some/dir" is. I searched around a little bit and found that "" ...
3
votes
2answers
778 views
`$XAUTHORITY` appears from 'nowhere' on su+tmux
When I switched from su+bash to su+tmux+zsh I noticed that I get $XAUTHORITY variable defined as /root/.xauthXXXXXX where XXXXXX are 6 random alphanumeric characters. With previous configuration X ...
2
votes
3answers
34 views
Can I reload variable in a watch command?
Part of my job involves some data handling. One of the tasks is to 'flatten' a set of directories (which we'll call Dir for now), and copy them to a new location called DirFlat.
This can take a long ...
2
votes
2answers
81 views
how to have find on a directory with a changing pattern?
I have a folder structure like this:
/domains/some-domain-1/applications/j2ee-apps
/domains/some-domain-2/applications/j2ee-apps
/domains/some-domain-3/applications/j2ee-apps
What is the best way ...
1
vote
1answer
277 views
error using “du” command
I am on the university machine trying to estimate the disk usage I have on my space I do
du -csh ./
from the begining of my account, but an error is given that ./Yesterday/Yesterday is not a ...
1
vote
2answers
3k views
Delete files in a directory that match a regexp, using a Mac terminal
How do I delete files in a directory that match a given regexp, or a similar solution, using a Mac terminal?
0
votes
1answer
160 views
difficalty to save traceroute output in shell variable?
I need to filter and store traceroute output in shell variable (array). But I am getting some unusual output.
As I try to explain in following lines.
My present directory is temp, having some ...
11
votes
4answers
2k views
Usage of dash (-) in place of a filename
For a command, if using - as an argument in place of a file name will mean STDIN or STDOUT.
But in this example, it creates a file with the name -:
echo hello > -
How can I make - in this ...
7
votes
2answers
402 views
What governs the limits of shell brace expansion?
In this example I refer to the expansion of a sequence of integers, but perhaps(?) the limits would be relevant to all aspects of brace expansion.. This more general view is also of interest to me.
...
6
votes
2answers
554 views
What is word splitting? Why is it important in shell programming?
I'm getting confused about the role word splitting plays in zsh. I have not been exposed to this concept when programming in C, Python or MATLAB, and this has triggered my interest of why word ...
6
votes
2answers
1k views
Invoking pushd in a shell script
I have a simple script that I want to invoke 'pushed' followed by another command. But the 'pushd' command inside the script doesn't seem to stick past the script.
What's a way to make this execute ...
5
votes
3answers
151 views
When was process substitution first introduced?
I'm curios to know how long process substitution has been around. What shell first had it? When did it get added to other shells?
5
votes
1answer
735 views
Korn Shell: End, pgup, pgdown, and delete key not working
So basically my End, Pageup/Pagedown, and Delete key are not working in ksh93. I'm running FreeBSD by the way.
My arrow keys are working, and so is my home key.
Those keys work when I put this in my ...
5
votes
2answers
2k views
what is meant by connecting STDOUT and STDIN?
This is a newbie question. I'm reading a book, it says:
Every process has at least three communication channels available to it: “standard
input” (STDIN), “standard output” (STDOUT), and ...
5
votes
2answers
4k views
How to change from csh to bash as default shell
I have csh as my default shell, as shown by echo $SHELL. I want to switch to bash as my default shell. I tried the following approaches to no avail:
chsh. I get:
chsh: can only change local ...
5
votes
3answers
3k views
Shell console clearing
Is there a way to clear up what is displayed on the shell console (in ubuntu linux) in just a single key or two? When I get flood of lines from grep and that exceeds the height of the window, I want ...
5
votes
4answers
1k views
Find a file in the path without “which”?
I am (somehow) able to run a script:
$ assemble.sh file
... [output]
But which can't find it:
$ which assemble.sh
which: no assemble.sh in ...
5
votes
1answer
555 views
Why root's default shell is configured differently with other normal user account's default shell?
As I know, root's default shell is configured csh and normal user's default shell is sh in FreeBSD. And in Ubuntu, root is dash, normal user is bash. (refer: What's the Ubuntu's default shell?)
Why ...
