Make multiple directories and put files into each one - bash

I've been attempting to mkdir multiple directories and the touch a set of files into each one.
I'm basically trying to combine the following:
mkdir ./file_directory{1..10};
then in each directory
touch ./file_directory{1..2} file_name{1..10};
When I execute touch it creates a the file_name{1..10} under ./
I've been looking around. Please forgive me for I have sinned and am new to not only bash, but coding/scripting in general. I'm attempting to create these files to learn scripting. Although this is technically my first project, I'm going to

You need to put / between the directory and filename to create a file in the subdirectory:
mkdir file_directory{1..10}
touch file_directory{1..10}/file_name{1..10}

Related

purpose of chdir with a single dot (cd .)

This might appear a noob question.
While working in bash, if we run cd ., it stays in the current folder.
I understand the functionality, however, I am not able to understand the rationale of this functionality?
What would be some practical ways to use this?
The primary use case I've seen for cd . is to test whether your file handle on the current directory is still valid.
If you're on a directory from a network share -- NFS, or the like -- it can be possible for directories to be remotely deleted, but for the local client to still believe they're accessible and in use.
cd . is a way to trigger an error if your handle on the current working directory is no longer valid.
This is the only "practical" case that came to my mind
$ cd .
cd: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
when your process has a current working directory referencing a directory that has been removed by another process.
That command has no functionality. But in a POSIX-compliant environment, if you add a -P option, then it has functionality: it resolves symlinks. So for example on a Mac, if you cd to a path with a symlink:
cd /System/Library/Frameworks/AppKit.framework/Versions/Current
...then do cd -P . ... you will point to:
/System/Library/Frameworks/AppKit.framework/Versions/C
. is a special file that represents the current directory.
There are plenty of things which make use of directories and it is sometimes useful to refer to the current directory.
Changing the directory to the current directory is not one of those.
A simple example where cd . fails:
mkdir my_error
cd my_error
rm -rf ../my_error
cd .
When the rm is embedded in a difficult script or can be done by some other cleanup process, is can be an useful check.
I use a build script which removes and recreates a directory.
The old directory disappears and new appears with new inode.
If, in one of my shells my $PWD is that reappeared directory and I notice
it became unusable (and I know it was recreated), I just
$ cd .
to get the (new) directory useable again and can continue my work there.

Compiling files in a directory into their own zip folder using a makefile script

I am currently taking a class where we submit homework through an online tool. However, it requires submissions to be made up of individually zipped files in order to compile them properly. This is time consuming when I sometimes make multiple submissions. I am trying to write a make file script to make a zipped copy of all files within the same folder. I then want to move those zipped files to a sub directory I create afterward called zippedFiles. This is what I have so far. The for loop line works when I run it directly in terminal but has the following error when I run make zip I get the following error: zip error: Nothing to do! (.zip) I am new to learning bash and make files and have been unable to research a solution on my own.
zip:
rm -f ./*zip #remove any extra zip copies.
rm -rf zippedFiles #delete old zippedFiles
for i in *; do zip $i.zip $i; done #zip each file ***not working
mkdir zippedFiles #remake new zippedFiles directory
In makefiles, you must use twice the $ to reference to a variable from the for loop.
for i in *; do zip $$i.zip $$i; done

How can the same move command behave differently depending on the context?

This should be trivial really, I have a script that compile an app then bundle it into a dmg.
The steps are:
make
mv app.app/ installer/artifacts/
createDMG
The problem is this runs within a CI system and it fail because it can't find the app.app in the artifacts folder. Indeed if I look at what's inside this folder I can only see a Content folder which is supposed to be app.app's child folder. Now I don't think that the command to move the app.pp folder is wrong because when I run all those 3 steps it works just fine...
I'm a bit confused now, how can 2 move command can have 2 different behaviors ?
If the folder installer/artifacts/ does not exist, move will rename app.app/ to installer/artifacts/
You could do the following instead:
make
mkdir -p installer/artifacts/
mv app.app installer/artifacts/
createDMG

how to make the bash script portable?

I have written a bash script which is dependent on current folder structure,
What should I do to make it runnable in any other folder(become portable)?
let me explain more: I want the script to delete all the files in current directory that the script is running,
or to delete all the files that are in the parent folder,
what is the solution?
cd "$(dirname "$0")"
Add this to the top of your script to have it cd to the directory the script is installed at. Then you can use relative paths and they'll be relative to the script dir and not the user's pwd.
If for whatever reason dirname isn't available, here's an equivalent statement that uses basename instead.
cd "${0%%/$(basename "$0")}"
Don't use absolute paths in your script.
we need more information to give you a proper answer...BUT some tips if you need to make it portable are to store any files that you need on a network share and simply make a new folder off of / such as /temporaryScriptFolder/ and mount the network share to that empty folder, allowing you easy access to any resources that are necessary.

sync/copy folders from source dir only if folders exist in target dir

I'm trying to write a bash script in ubuntu to do a copy of some files.. I'm working on a small Android project, where i'm translating the apps each week. I'm VERY new to bash scripting, so please bear with me ;)
I want my script to check the target directory and see if my source directory contains the same folders. If it does, it should copy (and overwrite if needed) my source folders to the target dir, preserving the structure. But also adding whatever extra files and folders i might have within those source folders.
Let's say i have folder1, folder2, folder3 in my source dir, but only folder1 and folder2 in the target dir. Then i only need folder1 and folder2 from the source dir copied to the target dir.
The content of the target dir changes often, that's why i need the check before it copies the folders/files over.
Btw, the folders in both source and target dir are named like: folder1.apk - it has an extension so it looks like a file..
Hope i provided enough info ;)
EDIT:
I ended up doing this:
for dir in `find * -maxdepth 0 -type d`; do
cp -r -f /source/$dir /destination
done
Don't know if it's the best way, but seems to do the job ;)
You would probably take a look at rsync tool, which has lot of options and easy to use (no need to use own scripts). For example, one of the options that will be useful in your case:
--existing skip creating new files on receiver
So, the following should do the job:
rsync -vur --existing ~/project/source /mnt/target/
And one of the possible benefits that you can sync files the same way through network if you will need to or even use it as a daemon to automatically sync files.

Resources