In Bash how to read the properties of a file? - bash

I am very new to bash scripting.
I need to make a bash script which finally copies files from one directory to another directory.
But target directory for all the files does not have be the same for all the files in the source directory. If the target directory does not exists then it must be created. The name of the new directory depends on the creation date of the concerning file. The format of the directory name is YYYY_MM_DD.
Therefor I must read the properties of the file, and then check if the target directory exists. If not, then the create the directory and copy the file. If it exists then copy the file.
How can I read the properties of a file? How can I create a new directory in format YYYY_MM_DD? How can I check if the directory already exists?
To know the target directory, the script needs to read the properties of every file in the source directory via a loop. How can I loop through the files in a directory?
Have I made myself clear? If not, ask me for more information.
Thank you.
Carlos Wiesemann

To read the last modification date file in a directory via a loop
ls | stat --format "%n - %y" *
or if you don't like the ls command, as Cyrus sugested, remove it ;-)
stat --format "%n - %y" *

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

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.

how to create a zip file of the cd in bash script

i want to create the zip file of the current working directory in bash script. AND Redirect the stdout of zip command to a file called zip-output.txt.
i have a current workind directory called "music" and my script populates it with a lot of content. thus after it is populated i want to convert it to "music.zip" . but the contents of the "music" directory shouldn't be altered or zipped.
/Users/xyz/Downloads/music
this is the path to the cd "music" if need be and the file "zip-output.txt" could be created at the path "/Users/xyz/Downloads"
ik this is easy but i am new. please guide me
Do you mean something like this?
zip -r music.zip music/ | tee zip-output.txt

Shell Script to rename file with string from another filename

I have been stuck with a task where I need to rename a file with a substring from another filename and then move it to a directory.
What I want to achieve here is as below
I have a file named dummy.txt and another file named 20150416demo.xml
What I want is the file abc.txt to be renamed with the date from the xml file i.e. dummy20150416.txt
The final step is to move it to a different location. Appreciate you responses
Thanks
To rename/concatenate and move according to what you want you can use this script:
#!/bin/bash
file="20150416demo.xml"
date=${file:0:8}
mv abc.txt /destination/path/dummy$date.txt

Getting directory of a file in Unix

I have a requirement where I need to copy some files from one location to other (Where the file may exist). While doing so,
I need to take a backup if the file already exists.
Copy the new file to the same location
I am facing problem in point 2. While I am trying to get the destination path for copying files, I am unable to extract the directory of the file. I tried using various options of find command, but was unable to crack it.
I need to trim the file name from the full file path so that it can be used in cp command. I am new to shell scripting. Any pointers are appreciated.
You can use
cp --backup
-b'--backup[=METHOD]'
*Note Backup options::. Make a backup of each file that would
otherwise be overwritten or removed. As a special case, `cp'
makes a backup of SOURCE when the force and backup options are
given and SOURCE and DEST are the same name for an existing,
regular file. One useful application of this combination of
options is this tiny Bourne shell script:
#!/bin/sh
# Usage: backup FILE...
# Create a GNU-style backup of each listed FILE.
for i; do
cp --backup --force -- "$i" "$i"
done
If you need only the filename, why not do a
basename /root/wkdir/index.txt
and assign it to a variable which would return only the filename?

Resources