creating a directory with files in it - bash script - bash

I would like to create a directory containing two files. The directory has the name of a given argument, therefore the name is not fixed. How can I reference this argument correctly?
The following code returns: "bad substitution". What does this mean?
for i in "$#" ; do
# create dir
mkdir "$i"
# create 2 files
touch ${"$i"}/file1
touch ${"$i"}/file2
done

This one is correct:
mkdir "$i"
Use the same syntax for touch:
touch "$i"/file1
It's also possible to use "${i}" but it's the same thing, just longer.

Related

Selectively create folders based on names in bash script

I have several files called as follow:
dosulepin3D_CID_5284550.pdbqt
protriptyline3D_CID_4976.pdbqt
These are small molecules. Now I want to create a file in the Results folder for each one of these molecules, ignoring the 3D_CID_5284550.pdbqt they all have behind, and have folders called:
dosulepin
protriptyline
I want to do this with a for loop, since I'm also performing some functions with these files. This is what I have:
DIR="/home/roy/MolecularDocking/VirtualScreening/Dockings"
cd $DIR
for ligand in ligands/*; do
echo $ligand
mkdir "/home/roy/MolecularDocking/Results/$ligand"
done;
But this obviuosly creates folders with the full name.
Something like this.
DIR="/home/roy/MolecularDocking/VirtualScreening/Dockings"
cd "$DIR" || exit
for ligand in ligands/*.pdbqt; do
echo "$ligand"
echo mkdir -p "/home/roy/MolecularDocking/Results/${ligand%3D*}"
done
Remove the echo from the mkdir if you're satisfied with the output.
See Parameter Expansion for more details.

Unix Bash Script Create Directory parent and child method

I'm trying to create directory using if condition statement, while running script i am not able to find any expected result from the script, but when i am running manually only mkdir command alone its creating as we expected; here the sample code.
#!/bin/bash
dir_path=/tmp/opt/app/software/{A,B,C,D}
if [[ -d $dir_path ]]; then
mkdir -p /tmp/opt/app/software/{A,B,C,D}
fi
can you please advise, how we can create this..
dir_path is a "list" of directory paths due to the {} parameter expansion. If you write this out:
dir_path=/tmp/opt/app/software/A /tmp/opt/app/software/B /tmp/opt/app/software/C /tmp/opt/app/software/D
This is what's being used in the test of the if statement.
Either you want to iterate over the list of sub directories, or just pass them to mkdir. mkdir simply won't create the directory if it already exists.
Your mkdir command actually expands out to:
mkdir -p /tmp/opt/app/software/A /tmp/opt/app/software/B /tmp/opt/app/software/C /tmp/opt/app/software/D
If you want to itterate and still do a check (which while needless in this example can still be useful other times.)
# Declare the variable `dirs` to be an array and use
# parameter expansion to populate it
declare -a dirs=(/tmp/opt/app/software/{A,B,C,D});
# Iterate over the array of directory names
for dir in ${dirs[#]}; do
if [ ! -d "$dir" ]; then
# The directory does not exsist
mkdir -p "$dir"; # Make the directory
fi
done

Run a command to make a directory through bash

I am creating a command that when executed will automatically make a folder on my desired location and put some files in it so I can get started quickly but for some reason I am not being able to create a directory. The goal is to pass a variable for the name of the directory to be created.
I have used mkdir and then pass the path and file name no errors are shown but the directory is not created. I even used eval.
#!/bin/bash
echo File name:
read file
cd ~/Desktop
mkdir $file
I expect the directory to be created and shown.
Use quotes:
mkdir "$file"
Example of what might happen if you do not use quotes:
file="hello world"
mkdir $file
This creates 2 directories "hello" and "world", and not the expected directory "hello world".

Unix Bash Alias Command

I am trying to simplify my work with the help of Alias commands in my bash shell.
Problem Statement:
I want to copy different files from different directories to one single folder. The syntax i am using here is as below
cp <folder>/<file> <path>/file.dir
Here I want to save the destination file with filename.directory for easy identification. To achieve the same, I have written the below alias.
Alias Script
cp $Folder/$fileName ~/<path>/$fileName.$Folder
OR
cp $1/$2 ~/<path>/$2.$1
Expected output,
cp bin/file1 ~/Desktop/personal/file1.bin
cp etc/file2 ~/Desktop/personal/file2.etc*
However, It's failing at parsing the source file. i.e. $Folder is not replaced with my first argument.
cp: cannot stat `/file1': No such file or directory
I am writing the above script only to reduce my command lengths. As I am not expert in the above code, seeking any expert help in resolving the issue.
Rather than using an alias you could use a function which you define in some suitable location such as .profile or .bashrc
For example:
mycp()
{
folder=$1
filename=$2
if [ $# -ne 2 ]
then
echo "Two parameters not entered"
return
fi
if [ -d $folder -a -r $folder/$filename ]
then
cp $folder/$filename ~/playpen/$filename.$folder
else
echo "Invalid parameter"
fi
}
There is no way a bash alias can use arguments as you are trying to do. However, perl based rename can probably help you here. Note that it will effectively mv the files, not cp them.
rename 's|([^/]*)/(.*)|/home/user/path/$2.$1|' */*
Limitations: You can only process the files in 1 sub-directory level.
So, below alias can work (with above limitation):
$ alias backupfiles="rename 's|([^/]*)/(.*)|/home/user/path/\$2.\$1|'"
$ backupfiles */*
You can make more sophisticated perl expression if you want to work with multi-directory-level file structure.
A directory contains some files say ~/Documents/file1.d contains newfile.txt
joe#indiana:~/Documents$ ls -l $file
total 1
-rw-r--r-- 1 joe staff 0 May 5 11:39 newfile.txt
Add the variable 'file' in .bashrc for example my .bashrc is shown here
alias ll='ls -la'
file=~/Documents/file1.d
Now whenever you copy to '$file' it will copy to file1.d directory under ~/Documents :)

Bash: Creating subdirectories reading from a file

I have a file that contains some keywords and I intend to create subdirectories into the same directory of the same keyword using a bash script. Here is the code I am using but it doesn't seem to be working.
I don't know where I have gone wrong. Help me out
for i in `cat file.txt`
do
# if [[ ! -e $path/$i ]]; then
echo "creating" $i "directory"
mkdir $path/$i
# fi
grep $i file >> $path/$i/output.txt
done
echo "created the files in "$path/$TEMP/output.txt
You've gone wrong here, and you've gone wrong here.
while read i
do
echo "Creating $i directory"
mkdir "$path/$i"
grep "$i" file >> "$path/$i"/output.txt
done < file.txt
echo "created the files in $path/$TEMP/output.txt"
78mkdir will refuse to create a directory, if parts of it do not exist.
e.g. if there is no /foo/bar directory, then mkdir /foo/bar/baz will fail.
you can relax this a bit by using the -p flag, which will create parent directories if necessary (in the example, it might create /foo and /foo/bar).
you should also use quotes, in case your paths contain blanks.
mkdir -p "${path}/${i}"
finally, make sure that you are actually allowed to create directories in $path

Resources