This question already has answers here:
Bash mkdir and subfolders [duplicate]
(3 answers)
Closed 6 years ago.
I'm sure this question has been asked elsewhere but I can't seem to phrase it in a way that returns a useful Google result.
I am creating a dozen directories that all have the same root path and I don't want to have to cd into it to be able to make these directories. The current command looks like something like, which is awful and repetitive:
$ mkdir frontend/app/components/Home frontend/app/components/Profile \
frontend/app/components/Post frontend/app/components/Comment
An ideal syntax would be something along the lines of:
$ mkdir frontend/app/components/{Home, Profile, Post, Comment}
Is there something like this already that I just haven't found? I don't want to have to run a for loop just to make a few directories.
Your wish is granted :-).
mkdir doesn't know and doesn't have to, but shells like bash or zsh understand the syntax {...,...,...}.
Just remove the spaces from your "along the lines of" and it works:
mkdir frontend/app/components/{Home,Profile,Post,Comment}
The shell will expand it to
mkdir frontend/app/components/Home frontend/app/components/Profile frontend/app/components/Post frontend/app/components/Comment
Since it is done by the shell, it works with any command.
Remove spaces around comma and use -p option:
mkdir -p frontend/app/components/{Home,Profile,Post,Comment}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 14 days ago.
This post was edited and submitted for review 14 days ago.
Improve this question
autojump works by maintaining a database of the directories you use the most from the command line. Then you can cd to directories via shortcuts, e.g. to jump to a directory that contains "foo", I can just call j foo instead of cd /full/path/to/foo.
$ pwd
/some/directory
$ j foo
/full/path/to/foo
I'm trying to understand how autojump is able to change the directory by calling cd inside its bash script. As far as I know, such a script is executed in a separate shell. Here's the part in autojump's code that calls cd.
For example, calling this doesn't change the directory outside of the script, so how is autojump able to achieve it?
# myscript.sh
#!/bin/bash
cd path/to/foo
$ pwd
/some/directory
$ ./myscript.sh
/some/directory
How do zoxide and autojump change the current working directory?
Normally, with cd.
As far as I know, such a script is executed in a separate shell.
So they are not scripts, they are shell functions.
How is it able to call the function from the terminal in a way that it changed the directory in the terminal?
Like so:
$ cat mycdfunction.sh
mycdfunction() {
echo "jumping to my place"
cd tomyplace
}
$ . mycdfunction.sh
$ mycdfunction
jumping to my place
$ pwd
myplace
This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 2 years ago.
I have an alias in bashrc that I use to generate a particular LaTeX document (the main command used is pdflatex) but I want to be able to execute it regardless of where I am. Unfortunately, it seems like you can't use use this command with an absolute path unless the path is 'under' your current directory, so I cd into the directory my .tex file is, run pdflatex, then try to cd back to my previous directory. In bashrc, I have my command as
alias nbr="cd ~/path/to/dir && pdflatex file.tex && cd $OLDPWD"
The command works fine outside of putting me back in my previous directory. For some reason, this command works perfectly fine in a regular bash shell, so I'm guessing there's some issue with what $OLDPWD is considered in the context of bashrc but I'm not sure. Any ideas?
#Cyrus's answer fixed it...should've used single quotes.
This question already has answers here:
Batch renaming files with Bash
(10 answers)
Closed 5 years ago.
Is it possible to rename multiple files that share a similar name but are different types of files all at once?
Example:
apple.png
apple.pdf
apple.jpg
Can I substitute the apple for something else, for example "pear"? If this is possible, what would the command be? Many thanks for your time!
You can do this in bash natively by looping over the files beginning apple and renaming each one in turn using bash parameter expansion
$ for f in apple*; do mv "$f" "${f/apple/pear}"; done
The for f in apple* finds all files matching the wildcard. Each filename is then assigned to the variable f
For each assignment to f bash calls the command mv to move (rename) the file from it's existing name to one where apple is replaced by pear
You could also install rename using a package manager like Homebrew and call
rename -e 's/apple/pear/' apple*
This question already has answers here:
Why do you need ./ (dot-slash) before executable or script name to run it in bash?
(9 answers)
Closed 5 years ago.
When I executeinit-hooks I get
bash: init-hooks: command-not found
here are the contents of init-hooks:
#!/bin/bash
set -e
printf '\ncopying hooks\n\n'
cp ./hooks/* ../../.git/hooks
When I execute cp ./hooks/* ../../.git/hooks from bash directly execution is successful.
(note this is the same command as what is in the script)
Proof of the files are in the directory and the results of execution:
Why does my script behave differently than the command/why is my script not found?
On the Linux systems (where bash comes from) the current directory is usually not included in the path for security reasons.
Run echo $PATH to check what directories are used to search for executables when they are provided in the command line without a path. The current directory (.) should not be there.
Run the script as ./init-hooks and bash will find it.
I suugest to run it following way
./init_hooks
or put fully qualified file name.
make sure to make the script executable
chmod +x ./init_hooks
This question already has answers here:
Get most recent file in a directory on Linux
(23 answers)
Closed 4 years ago.
I need to get the latest directory name in a folder which start with nlb.
#!/bin/sh
cd /home/ashot/checkout
dirname=`ls -t nlb* | head -1`
echo $dirname
When the folder contains many folders with name starting nlb, this script works fine, but
when there is only one folder with name starting nlb, this script prints the latest file name inside that folder. How to change it to get the latest directory name?
Add the -d argument to ls. That way it will always print just what it's told, not look inside directories.
#!/bin/sh
cd /home/ashot/checkout
dirname=$(ls -dt nlb*/ | head -1)
echo $dirname
As the other answer points it out, you need the -d to not look inside directories.
An additional tip here is appending a / to the pattern. In the question you specified to get the latest directory. With this trailing / only directories will be matched, otherwise if a file exists that is the latest and matches the pattern nlb* that would break your script.
I also changed the `...` to $(...) which is the modern recommended writing style.