Bash is a free shell for Unix-like operating systems, from the GNU Project

learn more… | top users | synonyms

11
votes
2answers
3k views

create mysql database with one line in bash

Is it possible to create an empty database with one line of command in bash? My newbie attempts have failed: mysql -uroot $(create database mydatabase;) I end up having to mysql -uroot create ...
10
votes
7answers
5k views

Why doesn't echo support “\e” (escape) when using the -e argument in MacOSX

When I try to print out some colored text using ANSI escape sequences via the built-in echo command, it seems that the \e escape sequence in the string I provide is interpreted literally instead of as ...
10
votes
8answers
6k views

How can I prepend a line number and tab to each line of a text file?

How can I append a line number and tab to the beginning of each line of a text file?
10
votes
3answers
3k views

How do I launch a bash script from Finder in OS X?

How do I launch a bash script from Finder in OS X? I'm OK working at the CLI, but my little script is not so loved by my GUI-bound colleagues.
10
votes
3answers
1k views

In Linux, how do I truncate command-line output?

If I grep -nr sumthin * in my source code directory, it also spews out very long lines from minified JavaScript or CSS files. I want to get just the first 80 characters per line. For example, a ...
10
votes
5answers
2k views

How to run sed on over 10 million files in a directory?

I have a directory that has 10144911 files in it. So far I've tried the following: for f in ls; do sed -i -e 's/blah/blee/g' $f; done Crashed my shell, the ls is in a tilda but i can't figure out ...
10
votes
1answer
416 views

What's an elegant way to copy the creation and modification dates of a file to another file?

How can I set the time information of a file to be the same as of another file on OSX / Linux?
10
votes
4answers
7k views

how to pipe command output to other commands?

Example: ls | echo prints nothing ( a blank line, actually ). I'd expect it to print a list of files. ls | grep 'foo', on the other hand, works as expected. What is do in these situations is ...
10
votes
3answers
7k views

linux merge folders: rsync?

I have two copies of a folder src/ dest/ I want to merge them, doing the following: If a file is only in src, I want it to be moved to dest If a file is only in dest, I want it ignored IE left ...
10
votes
3answers
727 views

Escaping apostrophe in single quotes string in bash

I don't understand how Bash evaluates escaping of apostrophe characters in single quoted strings. $ echo ''\''Hello World'\''' 'Hello World' #works $ echo '\'Hello World\'' > # expects you to ...
10
votes
2answers
3k views

Putty SSH: CTRL+Cursor keys don't work skip to previous/next word

When I am connected to my local Ubuntu dev server with putty (from my Windows 7 box) via SSH, the following key combinations don't work to move the cursor forward and backward on word boundaries: ...
10
votes
2answers
124 views

How to move folder in unix using mv, only when target is on the same drive

I need to move a folder using unix mv command, but only if the target is in the same drive or the same filesystem. What would be the best approach?
10
votes
4answers
5k views

How do I fix my prompt in emacs shell-mode?

I'm doing some programming on a colleague's machine. He has a version of emacs (23.1.1) I haven't used before. My problem is that when I go to shell-mode, my bash prompt looks like this: ...
10
votes
2answers
1k views

How do I override the bash “noclobber” option?

The bash noclobber option prevents one from overwriting files with redirection. But sometimes I really want to. csh has a similar option and it can be over-ridden by putting a ! before the filenames. ...
10
votes
2answers
388 views

How to prevent Bash from altering history?

If I press the Up or Down arrows on my keyboard and then modify something from my history, it's changed forever. Even if I press Ctrl-C and then try to bring it up again, it's still changed -- I've ...
10
votes
4answers
20k views

How to automatically execute a shell script when logging into Ubuntu

How do I get a script to execute automatically when I log in? Not when the machine starts up, and not for all users, but only when I (or any specific user with the script) login via the GNOME UI. ...
10
votes
1answer
849 views

Reload file in vim

When I have a file open in vim, in this case a log file, I'd like to trigger a refresh and load any file changes into my existing vim session. I often use tail -f on log files, but as I'm using java ...
10
votes
4answers
634 views

Is there any way to save a keyboard macro in bash?

I would like to permanently save a keyboard macro that I've recorded using C-x ( and C-x ). Is there an easy way to "see the source" for my newly recorded macro so that I can bind it to a keyboard ...
9
votes
6answers
3k views

How can I move around the bash commandline efficiently?

So I use the up in my shell (Bash on OSX or Ubuntu, mostly) but some of the time I know that I want the stuff after the cursor's current location. Is there any way to have the line I'm on complete ...
9
votes
5answers
10k views

how to execute command after current running command in bash?

I'm downloading file with wget, and I want to turn off my computer after downloading will be finished, but I need to go somewhere now :) Is it possible to execute command in bash exacly when process ...
9
votes
3answers
1k views

Calling a script with ./bla.sh vs. . bla.sh

Can anybody explain to me what the shell does in the two examples A) and B) below? It obviously behaves differently, but I can't find out why the output is different. Example: Let's have a script in ...
9
votes
3answers
491 views

What does a bare < do?

bash does not seem to complain if you run a command like $ < some-file-that-exists Nothing seems to happen. You could also use $ <<<"any string" and even process substitution. So ...
9
votes
3answers
11k views

Change DNS Server From Terminal (or script) on Mac OS X

How can I change my DNS server from the Terminal on Mac OS X? [I need this because my DNS is not working correctly with my VPN. Sometimes it's using the DNS for my main connection, and sometimes it's ...
9
votes
3answers
19k views

How can I change shell in Ubuntu?

I have a Ubuntu Server VPS and I want to use /bin/bash/ as my shell. How can I change my shell? I have root access but I don't work as root. So dash is my default shell now. I have read How do I make ...
9
votes
5answers
5k views

Writing “tail -f” output to another file

As a continuation from my last post where I have used grep & tail -f to find occurences of "rare" events. I would like to record this in another file. I've tried turning tail -f log.txt | ...
9
votes
3answers
3k views

How to cd to a directory that contains a space in its name?

I have a directory called "Reader 0.5" in my Desktop on Mac Os X. When to access the directory in terminal, I am using below code: cd /Users/niho/Desktop/Reader 0.5 but it throws: No such file or ...
9
votes
4answers
2k views

Understanding pipe in bash

I dont quite understand how pipes work in bash. I know that it takes an output from one command as the input in another command. What an output is i can get because it's what the command prints out ...
9
votes
7answers
8k views

Making BASH script `for` handle filenames with spaces (or workaround)

Whilst I have been using BASH for several years, my experience with BASH scripting is relatively limited. My code is as below. It should grab the entire directory structure from within the current ...
9
votes
5answers
16k views

tar: Exiting with failure status due to previous errors

I have written a little script that tars and compresses a list of directories + files. The script appears to run succesfully, in that a useable .tar.gz file is created after the script runs. ...
9
votes
2answers
15k views

echo text with new line in bash

I would like to append text to a file. So I wrote in bash echo text >> file.conf However it doesn't leave a new line. So I can only do this once. How do I add a new line?
9
votes
2answers
16k views

How to execute a bash script?

So I've written my first bash script: #!/bin/bash echo 'hello world!' exit I know it has the right location to bash and is executable: $ which bash /bin/bash $ chmod +x myscript.sh Now I want to ...
9
votes
5answers
16k views

Test if element is in array in bash

Is there a nice way of checking if an array has an element in bash (better than looping through)? Alternatively, is there another way to check if a number or string equals any of a set of predefined ...
9
votes
7answers
849 views

Last parameter of last command in bash in vi-mode

I have been convinced (over at Stack Overflow) to use my beloved bash in vi mode. So far I got used to it quite well and I like it. However I really do miss one feature: In emacs-mode, you can enter ...
9
votes
4answers
4k views

Find files filtered by multiple extensions

What is the correct syntax for: find . -type f -name \*.\(shtml\|css\) This works, but is inelegant: find . -type f -name \*.shtml > f.txt && find . -type f -name \*.css >> f.txt ...
9
votes
3answers
7k views

Looping Through Subdirectories and Running a Command in Each

I have a set of repositories sorted into directories based on their VCS (Git, Mercurial, SVN). With Subversion I was able to run svn update * in the parent directory and it would loop through each ...
9
votes
3answers
665 views

How can I save the current contents of less to a file?

If I've piped the results of a command to less and then decided that I want to save the contents to a file, is this possible? I've tried setting a mark a at the end of the buffer, and then returning ...
9
votes
2answers
9k views

Open gnome terminal programmatically and execute commands after bashrc was executed

For starting my dev environment I wrote a little script. One part is to open a gnome terminal with several tabs where automatically some commands should be executed. Some of those commands depend on ...
9
votes
2answers
4k views

Linux Shell: Copy output into clipboard w/o using the Mouse?

Is it possible to copy the output into the clipboard without using the mouse? For example, I would like to do something like this: $ pwd >> clipboard
9
votes
3answers
1k views

Bash shell tab completion, don't expand the ~

I use the Tab key a lot when I use the shell (bash). But I'm getting annoyed that ~ always gets expanded to /home/"user". I don't think it's always been like this; is there any way to stop this ...
9
votes
1answer
8k views

Ubuntu terminal, How do I “tab” (autocomplete) a filename window-cmd-style(cycle options)?

Basically i want to change ubuntu terminal functionally, so that i can cycle thru all the options when i tab(on the cmd line row) and not list the options as it is per default? Im using bash, ubuntu ...
9
votes
4answers
1k views

Bash history loss

I like to keep a lot of history, so I have histappend set in my .bashrc. Most of the time everything works fine, with history built up from many shells appending. However, every once and a while, I'll ...
9
votes
1answer
2k views

What does the 'rc' in `.bashrc`, etc. mean?

Well, this is embarrassing. I feel like I just realised I don't know a loyal colleague's name after working with them for 10 years. "Hey, er... *cough!*, thanks for that great job you did on setting ...
8
votes
9answers
973 views

Linux & Shell - Is Shell a must?

A total newbie question. Why do we need a shell in Linux? As an example when I type - find . -name xy* - I was told that the shell takes this input and calls the find command (making sure the wild ...
8
votes
5answers
366 views

Give all files a .jpg extension

I have a directory and some of the files' extensions are in uppercase (.JPG) instead of lowercase. I want to make sure every file has .jpg as its extension. How would I do this from a shell prompt?
8
votes
5answers
1k views

How can I still see the 'man' text after I quit man?

I typically use tcsh or bash and often want to use 'man' to review a command's options. Currently when I quit man or ctrl-C, the man text disappears and I see the scrollback buffer that was there ...
8
votes
4answers
284 views

Unix tools: what if a file is named minus something?

I'm always wondering: most GNU/Unix tools take options in the form "minus something", sometimes followed by an argument. What if you got a file named minus something? $ ls -f $ rm -f $ ls -f $ mv -f ...
8
votes
4answers
881 views

Bash Shell Tutorials

What are some good bash tutorials? I would like to learn more about the bash shells commands and how to use them.
8
votes
7answers
432 views

Is there a way to jump to an earlier part of a command line command in the Mac Terminal?

I'll often find myself writing out long commands in the bash shell - things with many arguments, web addresses, routes, etc and so on. Every so often I will realize I forgot to, say, put my "bundle ...
8
votes
2answers
263 views

How would you delete a folder called * in / from bash?

Let's say i had a folder named * under /. I know that common commands like rm -rf * won't work. Any help?
8
votes
4answers
2k views

A command before every bash command

Does anyone know a mean of putting a 'time' command before every command in a bash session?

1 2 3 4 5 62