Replace each directory with the sole regular file it contains - bash

I need to rename files in multiple directories to the name of the parent directory and move them up one directory and the delete the empty directories.
Sample structure:
/export
exp_20210101
3747-46473-328383-5555
exp_20210102
4533-45323-354345-5366
Desired result:
/export
exp_20210101
exp_20210102
3747-46473-328383-5555 renamed to exp_20210101
No extensions (Linux)
I prefere to do it with a bash script.
Already tried several samples (similar questions), but they're not working for my case.

I need to rename files in multiple directories to the name of the parent directory and move them up one directory and the delete the empty directories.
I don't think that's possible, you can't have a regular file and a directory with the same name in the same directory.
You should move each file to the same level as its parent without changing its name instead (assuming a file with the same name may not exist there). Then you can remove the parent and rename the file to its name.
for f in /export/*/*; do
echo mv "$f" "${f%/*/*}"
echo rmdir "${f%/*}"
echo mv "${f%/*/*}/${f##*/}" "${f%/*}"
done
Drop echos if the output looks good.

Related

how to create a directory with suffix .dat and also add suffix to all the files in that directory to .dat

I want to create a directory that suffixes .dat.
I want to write a shell script that creates a directory with the same name which I will give on the command line.
Example:
I have created a directory source_dir and in this directory, I have two files i.e
file1.txt file2.sh
When I run the .sh file
bash demo.sh source_dir
So the shell script in demo.sh should take care of the below things:
Creates a directory with the same name but add a suffix to it i.e .dat
Move all the files from source_dir to source_dir.dat and also add a suffix to all the files to file1.txt.dat file2.sh.dat
My attempt:
In a shell script how I will be able to create a directory with the same name as given on the command line?
To suffix the file names that we can achieve using
for file in source_dir.bak/*; do mv -- "$file" "$file.bak"; done

Copy files to their corresponding destination folders

I have many files in a folder with different extensions (.txt, .ascii, .mat). I want to move them to the destination folder which would be same as file names.
For example:
I have files like a.txt, a.ascii, b.txt, b.ascii, b.mat.
I want to make folder first in the name of a and b, then I want to move files a.txt,a.ascii to folder a and b.txt,b.ascii,b.mat to folder b.
I tried the code as follows. However I need an automatic way to make folder and move the files to it.
#!/bin/sh
mkdir a b
for file in $(<list.txt)
do
cp "$file" a
done
Seems that this Bash script can do the job
#!/usr/bin/env bash
for file in $(<list.txt); do
dirn="${file%.*}"
mkdir -p "$dirn"
cp "$file" "$dirn"
done

I want to move files in Shell

I have two folder xyz and Bin. I want to move xyz directory and it's sub directory as well to Bin folder.
I want output like this:
Bin/xyz/q.html
/images/0.gif
How can do move files and subdirectory and I will get that type of output.
Move (mv) can be thought of as rename. If mv is used to move a file from one drive to another, then a copy will be made and the original will be deleted.
If the file is a directory (folder), then all of the contents of that directory are also moved.
$ mv -v xyz Bin
moves the file called xyz, no matter what kind of file it is, even if it is a directory, into the existing directory called Bin. If Bin does not exist, then the move command will rename xyz to Bin.
If you only want to move a few files, for example, Bin/1.html is to be moved to Bin/xyz/1.html, then file being moved is a file called 1.html (not a directory called bin or xyz).
$ mv -v Bin/1.html Bin/xyz/1.html
will perform the move if the Bin/xyz exists and is a directory. If directory Bin/xyz does not exist, then this mv command will error because mv does not create parent destination directories.
$ mkdir -v Bin/xyz
will create an empty directory called xyz in an existing directory called Bin. Then files can be moved into the existing Bin/xyz directory.
The -v (--verbose) tells mv and mkdir to report what happened. The output that they produce given a success is what -v does. The most common way to list the content of a directory is ls, but the indentation that you desire will require some programming.
$ man mv
$ man mkdir
will show all the features of the mv an mkdir commands.

cp hundreds of files to different directories

The title not might be the best, I apologize, I'm rather new to scripting.
I'm trying to copy 2 files from each directory and place these compounds in a separate directory that only shares directory names.
for clarity;
/path/to/directory/all/variable_directories/
Inside this directory will be multiple files, I need 2 files which will have the same name in every individual variable directory.
I am trying to copy these 2 files from each individual variable directory and put them in variable directories based on the basename of /variable_directory/
the copy destination will be;
/path/to/magical/shit/subset/set_with_variable_name/variable_directories/
Only some of the destination directories are located in each /set_with_variable_name/
The script will need to be able to go through each /set_with_variable_name/ until it finds the directory that shares the basename of the directory that these files are originally being cp'd from
There's about 100 directories
to cp from and to and about 200 files total that need to be copied and sorted appropriately.
I can get it to cp ALL the files to the SAME directory using;
#! /usr/bin/env bash
for i in */;
do cd $i;
cp filename /path/to/destination/;
cp other_filename /path/to/destination/;
cd ..;
done;
It's the sorting the files to the correct destinations that I am completely lost at.
I appreciate any help, I'm a novice to this type of scripting
Looks like you need something like that.
# looping through all the variable directories
for var_dir in <path1>/all/*; do
# creating the destination directory if not exists
mkdir -p "<path2>/set_with_variable_name/$(basename ${var_dir})"
# copying the first file
cp "${var_dir}/filename1" "<path2>/set_with_variable_name/$(basename ${var_dir})/filename1"
# and the second
cp "${var_dir}/filename2" "<path2>/set_with_variable_name/$(basename ${var_dir})/filename2"
done

rename multiple files bash in a subdir

I am trying to use rename but I have several subdirs. The expression I am trying to use is this:
rename 's/ZAUQ-F24MS-SC12-F01-5C\/R44.wav/wav\/2012.wav/' *.wav
I want to rename the file ZAUQ-F24MS-SC12-F01-5C/R44.wav to wav/2012.wav and all files are *.wav files.
I am able to do this from inside the directory but I have multiple directories and a mapped to and from list. Can rename do this or should I be using something else?
Apparently you are moving a file from one directory (ZAUQ-F24MS-SC12-F01-5C) to another (wav). Why not mv then:
mv ZAUQ-F24MS-SC12-F01-5C/R44.wav wav/2012.wav

Resources