Concatenate two files from separate folder into multiple folder - unix

I have folder containing files a1_1, a1_2, a2_1, a2_2, a3_1, a3_2 and folder in another directory containing b1_1, b1_2, b2_1, b2_2, b3_1, b3_2. Want to combine them into one new folder like this:
1_1 (Folder)
a1_1
b1_1
1_2 (Folder)
a1_2
b1_2
2_1 (Folder)
a2_1
b2_1
2_2 (Folder)
a2_2
b2_2
I am using UNIX, doing it one by one will be troublesome since I have 300 pairs of file that should be combined. Please help

You can easily do it using basic bash scripting, here is an example on how to do it with comments explaining the steps.
#!/bin/bash
source_dir_a=$1 // Directory containing a files given as first argument
source_dir_b=$2 // Directory containing b files given as second argument
a_files=$(ls $source_dir_a) // Save list of files in provided a dir
for a_file in $a_files; do // Loop over the files in the a dir
echo "a_file=$a_file"
folder=$(echo $a_file | sed -e 's/^a//g') // Remove the leading "a" of a files using a regex for the storing folder name
echo "folder=$folder"
b_file=$(echo b$folder) // Adding leading "b" to folder name to have b_file name
echo "b_file=$b_file"
mkdir $folder // Create output folder
mv $source_dir_a/$a_file $source_dir_b/$b_file $folder // Move the a and b files in output folder
done
What's missing is some error checking, especially if some expected files are not present.

Related

Rename files in different folders removing numbers from file names

I'm trying to remove numbers from file names stored in different folders.
Specifically, I have 100 folders named: my_folder1, my_folder2, my_folder3,..., my_folder100.
In each folder there are files named: my_folder1.txt for my_folder1, my_folder2.txt for my_folder2, my_folder3.txt for my_folder3, ...my_folder100.txt for my_folder100. I need the following output:
my_folder.txt for my_folder1, my_folder.txt for my_folder2, my_folder.txt for my_folder3, ..., my_folder.txt for my_folder100. In other words I need to remove the numbers from file names in each folder. I used the following code:
for file in `find . -name 'my_folder*.txt'`; do
mv $file ${file/+([0-9]).txt/.txt}
done
but the numbers are still there.
Can anyone help me please?
Best
Are you looking for:
for file in */*.txt; do mv $file $(dirname $file)/my_folder.txt; done

Move files in folders according to files and folders name

I have a list of files and a list of folders in which I would like to move the files.
In other words, I have files named like: a_myfile.txt and a folder named "a", then file: b_myfile.txt and a folder named "b", then c_myfile.txt and a folder named "c". I would like to move the file a_myfile.txt in the folder named "a", then the file named b_myfile.txt in the folder named "b" and so on. I have thousand of files and thousand of folders so it is impossible to move such files by hand.
Using a loop, use shell parameter expansion to get the foldername, create it and move the file.
for i in *.txt; do
mkdir -p "${i%%_*}"
mv "${i}" "${i%%_*}"
done
I would have used the directory iterator class but some few PHP installations don't have SPL installed. So I'll just use a global solution.
Here is some code that will scan a directory and the file names, store them in an array then check the first characters before the underscore then move it accordingly.
$directory = '/path/to/my/directory';
//to get rid of the dots that scandir() picks up in Linux environments
$scanned_directory = array_diff(scandir($directory), array('..', '.'));
foreach($scanned_directory as $filename)
{
$f = explode("_", $filename);
$foldername = $f[0];
//Create directory if it does not exists
if (!file_exists($foldername)) {
#mkdir($foldername);
}
//Move the file to the new directory
rename($directory. "/". $filename, $directory. "/". $foldername. "/". $filename);
}
Note: the new folders will be inside the old directory. You can customize it to create and move the new folders to a folder that you wish easily.

Rename many images with names from file

I have a file with a list of names. Let's call it nameFile. For example:
John Doe
John Benjamin
Benjamin Franklin
...
I also have a folder of pictures. The pictures are named like:
pic001.jpg
pic002.jpg
pic003.jpg
...
I want to rename each picture with the corresponding name from the nameFile. Thus, pic001.jpg will become 'John Doe.jpg', pic002.jpg will become 'John Benjamin.jpg', etc.
Is there an easy UNIX command to do this? I know mv can be used to rename, I'm just a bit unsure how to apply it to this situation.
Mostly people do it by writing a simple shell script.
These two links will help you to do it.
Bulk renaming of files in unix
Rename a group of files with one command
The mv is a Unix command that renames one or more files or directories. The original filename or directory name is no longer accessible. Write permission is required on all directories and files being modified.
mv command syntax
You need to use the mv command to rename a file as follows:
mv old-file-name new-file-name
mv file1 file2
mv source target
mv [options] source target

Powershell Script to rename TXT file based on certain criteria

I realise it is likely the each of the requirements listed below is available individually within the forum here but I am struggling to bring it all together (if at all possible!).
Hoping someone has the patience and time to point me in right direction to make this happen.
What I need to do is the following:
Scan a directory (and all sub-directories) for a particular filename
NOTE: Whilst there are many files within the sub-directories with the filename in question, we only wish to target those in a sub-directory with a suffix of JERRY
ie. In the below example the files indicated by the arrow would be targeted
ONE\NEW1-JERRY\FILENAME.TXT <----
ONE\NEW1-TOM\FILENAME.TXT
ONE\NEW1-SYLVESTER\FILENAME.TXT
TWO\NEW2-JERRY\FILENAME.TXT <----
TWO\NEW2-TOM\FILENAME.TXT
TWO\NEW2-SYLVESTER\FILENAME.TXT
THREE\NEW3-JERRY\FILENAME.TXT <----
THREE\NEW3-TOM\FILENAME.TXT
THREE\NEW3-SYLVESTER\FILENAME.TXT
FOUR\NEW4-JERRY\FILENAME.TXT <----
FOUR\NEW4-TOM\FILENAME.TXT
FOUR\NEW4-SYLVESTER\FILENAME.TXT
When file is found matching the filename and is within the sub-directory listed above take a copy of the file (to remain in same directory) & rename based on the following criteria:
a) Created date/time
b) Certain content within the file
The content in the file is always located on ROW 8 and it is the first 9 characters
Original filename: FILENAME.txt
Finished Product: FILENAME-20121129#1300-123456789.txt
Thanks in advance!
you should tell us what have you tried so far and what are your mains problems...
try this :
(remove the -whatif flag to actually copy files)
#list dir & subdirs
ls c:\ -recurse |
Foreach {
#find subdirs named JERRY
if($_.PSIsContainer -and $_.name -match "JERRY"){
ls $_.fullname -filter 'FILENAME*' |
Foreach{
$fcontent=get-Content $_.FullName
$content=$fcontent[7].Substring(0,9)
$newName=Get-Date -UFormat "%Y%m%d"
$destination="$($_.Directory)\$newName#$content.txt"
write-verbose $destination
Copy-Item $_.FullName -Destination $destination -WhatIf
}
}
}

Unix cp command destination = . (dot)?

What does . (dot) mean as the destination of the cp command?
For example:
cp ~dir1/dir2/dir3/executableFile.x .
When this executes it copies the file successfully with the correct file name, but I'm wondering is this what a destination of '.' will always do or is there another purpose?
Within the reference material I've seen, dots are used in front of files to indicate 'hidden', but in that has no relation to the command above.
dot represents the current directory
while dotdot is the parent directory.
As EvilTeach's answer says, . is the current directory, and .. is the parent directory.
There are basically two ways to use the cp command:
cp file1 file2
will copy file1 to file2, creating file2 if it doesn't exist or (depending on permissions) possibly clobbering it if it does.
The other way is:
cp file1 file2 ... dir
where dir is an existing directory. With this form, you can specify one or more files, and they'll all be copied into the specified directory dir with their existing names.
(This can be a pitfall sometimes; cp foo bar behaves very differently depending on whether there's an existing directory named bar.)
As you mention, files (including directories) whose names start with . are hidden. What this means is that (a) the ls command won't list them (unless you use the -a or -A option), and (b) a shell wildcard such as * or *.txt will omit them. (GUI directory managers such as Nautilus may also omit them, depending on your settings.)
This applies to the current directory . and the parent directory ... ls won't include the . and .. entries in its output; ls -a will.

Resources