I want to write a command that:   takes another command as its arguments does a 'cd' to a temporary folder executes the command passed in the arguments removes the temporary folder afterwards   Something like this:  run_in_temp_dir ~/bin/my_script_that_creates_lots_of_files.sh arg1 arg2   But it would make sense that such a command already exists. So my question is, does such a command exist?  Edit: apparently people think this is a duplicate, but I specifically asked for the possible existence of a wrapper command. And yes I am perfectly capable of writing this script myself, I just don't want to reinvent the wheel if not necessary.
121060
Quite often, we run an executable that needs to write / read some temporary files. We usually create a temporary directory, run the executable there, and delete the directory when the script is done.  I want to delete the directory even if the executable is killed. I tried to wrap it in:  #!/bin/bash dir=$(mktemp -d /tmp/foo.XXXXXXX) &amp;&amp; cd $dir &amp;&amp; rm -rf $dir /usr/local/bin/my_binary   When my_binary dies, the last process the kernel will delete the directory, as the script is the last process holding that inode; but I can't create any file in the deleted directory.  #!/bin/bash dir=$(mktemp -d /tmp/foo.XXXXXXX) &amp;&amp; cd $dir &amp;&amp; rm -rf $dir touch file.txt   outputs touch: file.txt: No such file or directory  The best I could come up with is to delete the temp directory when the process dies, catching the most common signals, and run a cleanup process with cron:  #!/bin/bash dir=$(mktemp -d /tmp/d.XXXXXX) &amp;&amp; cd "$dir" || exit 99 trap 'rm -rf "$dir"' EXIT /usr/local/bin/my_binary   Is there some simple way to create a really temporary directory that gets deleted automatically when the current binary dies, no matter what?
8187
Writing my first script so I'm sure this is a basic question, but can someone please explain to me why I can:  cd ~ cd bin cd ~/bin cd 'bin'   But not  cd '~' cd '~/bin'   I need to cd to a directory path with a space in one of the directory names, so I need the quotes (it's Windows Program Files under wine). I can get around it with two cd commands, but why can't I put ~ in quotes?  If I type cd '~' (or cd "~") I get:  bash: cd: ~: No such file or directory
112544
What's wrong with the commands below?  $ var1="~/Music/$(date +%d%m%y)" $ echo "$var1" ~/Music/240118 $ mkdir "$var1" mkdir: cannot create directory ‘~/Music/240118’: No such file or directory   However  $ mkdir ~/Music/240118   works.  Never thought I would ask such questions after years of using bash...
93109
I have a directory called "foo bar baz blob". How can I  $ cd "foo bar baz blob"   I have tried with quoting (" and ') and with escaping the blanks (\ ). It does not work. Tab-completion does not work either.  I also have to issue commands with filename arguments that contain spaces. How do I do that? Even MidnightCommander (mc) fails in doing this.  EDIT  Over one year later, I finally found the source of my problem. I have overloaded the builtin cd with a function that automatically lists the files of the changed directory:   $ type cd cd ist eine Funktion. cd ()  {      if builtin cd $1; then         ls;     fi }  $ cd Interner\ Speicher/ bash: cd: Interner: Datei oder Verzeichnis nicht gefunden  $ builtin cd Interner\ Speicher/  $ pwd /run/user/1000/gvfs/mtp:host=%5Busb%3A002%2C018%5D/Interner Speicher  $ ls Alarms   DCIM      Movies  Notifications  Podcasts   SmsContactsBackup Android  Download  Music   Pictures   Ringtones   The solution is easy: I just have to put double quotes around the $1:  if builtin cd "$1"; then   Ooof. Thank you.
66951
I have this script:  echo $HOME cd $HOME cd / cd /usr/local/src/   When I run it like this  . script.sh   I get this output:  /home/user : No such file or directory : No such file or directory : No such file or directory   If I run it normally (I added #!/bin/bash before the first line)  sh script.sh   I get this output:  : not found.sh: 2: script.sh /home/user script.sh: 3: Can't cd to /home/user script.sh: 4: Can't cd to / script.sh: 5: Can't cd to /usr/local/src/   The script was run from $HOME directory each time. If I run each command one by one from shell, it executed without problems (invoking cd $HOME from within $HOME simply didn't change directory).  What must I do to make this work?
18999
I have a small script that loops through all files of a folder and executes a (usually long lasting) command. Basically it's  for file in ./folder/*; do     ./bin/myProgram $file &gt; ./done/$file done   (Please Ignore syntax errors, it's just pseudo code).  I now wanted to run this script twice at the same time. Obviously, the execution is unnecessary if ./done/$file exists. So I changed the script to  for file in ./folder/*; do     [ -f ./done/$file ] || ./bin/myProgram $file &gt;./done/$file done   So basically the question is: Is it possible that both scripts (or in general more than one script) actually are at the same point and check for the existance of the done file which fails and the command runs twice?   it would be just perfect, but I highly doubt it. This would be too easy :D If it can happen that they process the same file, is it possible to somehow "synchronize" the scripts?
75545
I just upgraded my Ubuntu 16.04 to 17.04 and found a little problem with cd command.  Let's say that I have two folders: album-01 &amp; album-02  In Ubuntu 16.04, if I do cd album* it will go to the first folder found album-01  But in new Ubuntu 17.04, if I do cd album* it result me -bash: cd: too many arguments  How to make cd in Ubuntu 17.04 like cd in Ubuntu 16.04?
25903
Many days ago, I found this useful bash alias (contents of my ~/.bash_aliases)  # aliases  # finds temporary files ending with '~' and deletes them alias rm~='find . -name '*~' -print0 | xargs -0 /bin/rm -f'   Now I tried to use rm~ after cd to some directory containing 3 files ending in ~  I got this error in terminal, and files did not get deleted  find: paths must precede expression: 1n.in~ Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]   Also when I tried rm~ from /home/me, it seems to be doing nothing. Or it might be taking a lot of time.  Please tell me why am I getting error, and how to fix it.  Thanks!
106548
Suppose I have a directory structure as follows  test test/a test/b   Now I want to execute a command, such that in the . folder I can execute a command on the basename of the files a and b.  So basically, I want something like this, which I naively tried  find test -type f -exec touch `basename {}` \;   Only, this does not result in empty files a and b in the parent directory. Suppose that my touch command can only take a single argument.  This will result in a directory structure like this  a b test test/a test/b   I know how to do this in a bash script, but I am interested in single command solution.
98935
I have a sample script (credit goes to ):  #!/bin/bash cat &lt;(cat /etc/lsb-release)   I save this script as somename.sh in my home directory.  Now, I try to open this file in three different ways:  sh somename.sh  bash somename.sh  ./somename.sh   So, I have two questions:   Why are the outputs for the above commands different though they are running the same script?   sh produces a Syntax error bash outputs the desired result ./ gives a Permission Denied error  Why is it necessary to give the script executable permission only when running the script using ./ and not necessary in the other cases?   Thanks in advance!  EDIT: The first part might be similar to the  linked one but, I did have a second question too.
100532
The runtime arguments are as follows: $1 is the path to the file containing the list of files $2 is the path to the directory containing the files What I want to do is check that each file listed in $1 exists in the $2 directory  I'm thinking something like:  for f in 'cat $1' do if (FILEEXISTSIN$2DIRECTORY) then echo '$f exists in $2' else echo '$f is missing in $2' sleep 5 exit fi done   As you can see, I want it so that if any of the files listed in $1 don't exist in the directory $2, the script states this then closes. The only part I can't get my head around is the (FILEEXISTSIN$2DIRECTORY) part. I know that you can do [ -e $f ] but I don't know how you can make sure its checking that it exists in the $2 directory.
111306
I want to remove certain files using find and -exec. But unlikely bash tells me, that I'm "missing" some argument.   find . -name *.png -exec rm {} /;   what do I miss?  same "missing argument" return for my attempt to rename some files:  find . -name ic_launcher.png -exec mv {} other_name.png /;   Can somebody tell me, what bash does miss, and why this command isn't successful?
75140
I have the following shell script that sums up all the arguments passed to it (space separated):  sum=0 for arg in "$@"; do     (( sum += arg )) done echo $sum   I have saved it as SumAll.sh. So, its supposed to function as follows:  sh SumAll.sh 2 4 6 8 20   or  bash SumAll.sh 1 -2 3 -3 -1   Both work on my local machine (MacBook), but when I try the same in a linux server, running the script with bash works, but with sh does not work. I get the following error:  sum: '+=': No such file or directory sum: arg: No such file or directory   Why is that? And how do I fix it?
77666
Let's say I'm in a directory which has some subdirectories, dir1, dir2, and dir3.  Each of these directories have a file foo and I would like to execute the same command on each foo in each of the directories and have that command executed from inside that directory.  If I were to do it "by hand", it would look something like this:  cd dir1 (execute on foo) cd ../dir2 (execute on foo) cd ../dir3 (execute on foo)   It is imperative that the command be executed from each of the directories.  foo is a batch scheduling script (for HTCondor, if you care to know) and must be executed from each subdirectory so that the output of the runs started by the scheduling scripts will end up in each subdirectory.  The question "", as far as I can tell, does not answer my question. The first answer to that question is more of a workaround which will not work in my case, and the second answer does not have enough of an explanation for me to know how to use it.
87437
I'm trying to beat my head against the syntax of this. I'm getting problems with whitespaces and I can't see if there's a better way to do this.  vardir=/home/user cd $vardir for i in $(ls "$vardir") ; do timestamp="$(stat -c %Y $i)" echo "$i" echo "$timestamp"  done   Result is similar no matter where I quote and I'm not sure what I'm doing wrong:  stat: cannot stat ‘Temporary’: No such file or directory Temporary  stat: cannot stat ‘Items’: No such file or directory Items   My goal is I want to grab the timestamps off of backup directories and touch the current ones to restore the original modified dates.
104672
I'm seeking a way to create directory and change my present working directory to newly created directory just by using a single command. How can I do this?  i.e Instead of doing  user@Computer:~$ mkdir NewDirectory user@Computer:~$ cd NewDirectory user@Computer:~/NewDirectory$    I want to do  user@computer:~$ **command** NewDirectory user@Computer:~/NewDirectory$   What can the command be?
68546
I have six directories with command files. These are /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin and /usr/local/sbin.  What are the differences between these? If I'm writing my own scripts, where should I add them?    Related: