I want to move files in Shell - 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.

Related

Getting permission denied while creating a directory and how to add suffix to .exe to each files in a directory

I want to create a directory say source_dir and add few dummy files without content into this directory like file1.txt, file2.sh, etc
Now I want to create another directory say destination_dir and move all the files from source_dir to destination_dir but all the files that got moved from source_dir should be suffixed by .exe
Example:
source_dir
file1.txt file2.sh
destination_dir should have output as
file1.txt.exe file2.sh.exe
What I have tried :
I used mkdir source_dir -> But getting error cannot create directory. Permission denied.
touch file1.txt file2.sh -> I thought to use this command to create the files without content but not able to create a directory itself.
Once error is resolved and files are created in source_dir then I will use
mv .* source_dir destination_dir -> To move all the files at once but for this command I am not sure whether this will work or not
Then how to suffix all the files with .exe is also challenging to me and got stuck.
Can someone help me resolving the error of create directory and how to add suffix to each files?
I used mkdir source_dir -> But getting error cannot create directory. Permission denied.
It seems you do not have permission to create a fodler here. You might use sudo mkdir source_dir, but is likely a better idea to make the folder in a directory where you have write access EG. $HOME.
Once error is resolved and files are created in source_dir then I will use mv .* source_dir destination_dir -> To move all the files at once but for this command I am not sure whether this will work or not
For moving use mv .* destination_dir from withing the source_dir. (IE, first cd source_dir then run the move command from above)
Then how to suffix all the files with .exe is also challenging to me and got stuck.
You will have to loop over the files and move them one by one.
for i in * ; do mv "$i" "${i}.exe" ; 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

Replace each directory with the sole regular file it contains

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.

problem with copying directory files to another directory

So I want to copy some files from one directory to another.
Essentially, I want to capture the directory path to a variable, say, "pathname" and use
"cp-r $pathname ." to copy file1 and file2 to to a new folder in which I made using mkdir and have cd'ed into (hence the "." as the second command line argument).
source_to_copy_from:
home/folder1/folder2/file1
home/folder1/folder2/file2
destination_to_copy_to:
home/newfolder1/newfolder2/file1
home/newfolder1/newfolder2/file2
I did:
pathname=$"$(pwd)"
//code to make the newfolder1 here
cp -r $"$pathname" .
But there appears to be nothing in the new pathname that was supposed to be copied in.
Also am using Mac bash
also quite beginner to bash

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

Resources