Move files to their respective folder in linux based on the date of the folder and the date of the file - linux

I'm fairly new to bash scripting and linux, and I have a folders with just dates such as
2012-11-20
2012-11-21
2012-11-22
2012-11-23
and I have files with the name data_11202012_randomnumbers_csv.
I would like to create a script that can move every single csv file to it's correct folder by matching the date on the file to the folder.
I've been just typing mv file path but i have 100s of files and I'm wondering if theres an easier way.
Any help would be appreciated.

The following should do it for you. I will explain with comments
for file in your_folder/*; do
# 1. Extract the numbers from the file name
dir="${file#data_}" # remove data_ prefix
dir="${dir%%_*}" # remove everything after first _
# 2. Rearrange the numbers into the desired format
dir="${dir:2:4}-${dir:0:2}-${dir:6:2}"
# 3. Move the file into the directory
mv file dir
done
Here you have a very useful bash cheatsheet where you can learn more about it. It illustrates all the variable expansions I've made in my snippet and more.

Related

Is there a Linux terminal command for creating .xlsx files from .bracken files in a loop in a new folder

Is there a loop I can use to create .xlsx files from .bracken files I currently have and channel them into an output folder?
All that I have now is to convert my .bracken files into .xlsx files using this code cat MG-ABCD12345-0.genus.bracken > MG-ABCD12345-0_genus_bracken.xlsx and files are going into my current working directory. I would like the output in a folder called bracken_excel_files which is located within my current working directory. I would prefer to use common commands such as for for the loop for easier understanding.
bracken appears to be generating/sharing the same output format as kraken, which I saw somewhere to be tab-delimited fields.
If that is true, then that is the essence of what CSV files are.
In that case, you don't need to use the "cat" command (CPU and I/O consuming). You simply need to rename the file with a ".csv" suffix (to make the file format explicitly visible for others), then import that into Excel or OpenOffice/LibreOffice Calc. Each of those tools offer different options for interpreting the input when you use the "Import" function to open the files.

How to find all files by extension?

I create this code that find for me all files that I have in folders, but I need that code to show me a full name of txt files (only txt) that string I insert there.
For example:
I insert "Alex"
and it searching for all txt files that I have, and if it find it will give me a full name of txt file (example: "redme.txt")
What I need to change in my code?
this my code:
the question in the link below may give you the idea
How to get the first file with a .txt extension in a directory with nodejs?
First, try to get all files in a directory. Then, Sort out the .txt extensions. Next, compare the filename to the given string in the argument.

Linux terminal script to create boilerplate files in current working directory with one varying word?

I have to create two boilerplate files, both of which always have the same content, with the EXCEPTION of a single word. I'm thinking of creating a command or something that I can run in the Linux terminal (Ubuntu), along with an argument that represents the one word which can vary in the files created. Perhaps a batch file will accomplish this, but I don't know what it will look like.
I will be able to run this command every time I create these boilerplate files, instead of pasting the boilerplate and changing the one word in the file that has to be changed.
These file paths relative to my current working directory are:
registration.php
etc/module.xml
A simple Python script that reads in the file as string and replaces the occurrence would probably be the quickest. Something like:
with open('somefile.txt', 'r+') as inputFile:
txt=inputFile.read().replace('someword', 'replacementword')
inputFile.seek(0)
inputFile.write(txt)
inputfile.close()

Getting files names inside a rar/zip file without unzip

Does anyone know if it is possible to get the name of files inside a rar/zip without having to unrar/unzip the file.. and if yes, is there a way to block it or make difficult..
Thanks
The file names in a zip file are visible even if the data is encrypted. If you want to hide the names, the easy solution is to zip the zip file encrypted.
Later versions of PKZip do have an option to encrypt the file names as well with –cd=encrypt. (cd means central directory.)
The -l flag to unzip(1) does just that:
-l
list archive files (short format). The names, uncompressed file sizes and modification dates and times of the specified files are printed, along with totals for all files specified.
unrar(1) has the l option:
l
List archive content.

Linux - Restoring a file

I've written a vary basic shell script that moves a specified file into the dustbin directory. The script is as follows:
#!/bin/bash
#move items to dustbin directory
mv "$#" ~/dustbin/
echo "File moved to dustbin"
This works fine for me, any file I specify gets moved to the dustbin directory. However, what I would like to do is create a new script that will move the file in the dustbin directory back to its original directory. I know I could easily write a script that would move it back to a location specified by the user, but I would prefer to have one that would move it to its original directory.
Is this possible?
I'm using Mac OS X 10.6.4 and Terminal
You will have to store where the original file is coming from then. Maybe in a seperate file, a database, or in the files attributes (meta-data).
Create a logfile with 2 columns:
The complete filename in the dustbin
The complete original path and filename
You will need this logfile anyway - what will you do when a user deleted 2 files in different directories, but with the same name? /home/user/.wgetrc and /home/user/old/.wgetrc ?
What will you do when a user deletes a file, makes a new one with the same name, and then deletes that too? You'll need versions or timestamps or something.
You need to store the original location somewhere, either in a database or in an extended attribute of the file. A database is definitely the easiest way to do it, though an extended attribute would be more robust. Looking in ~/.Trash/ I see some, but not all files have extended attributes, so I'm not sure how Apple does it.
You need to somehow encode the source directory in the file. I think the easiest would be to change the filename in the dustbin directory. So that /home/user/music/song.mp3 becomes ~/dustbin/song.mp3|home_user_music
And when you copy it back your script needs to process the file name and construct the path beginning at |.
Another approach would be to let the filesystem be your database.
A file moved from /some/directory/somewhere/filename would be moved to ~/dustbin/some/directory/somewhere/filename and you'd do find ~/dustbin -name "$file" to find it based on its basename (from user input). Then you'd just trim "~/bustbin" from the output of find and you'd have the destination ready to use. If more than one file is returned by find, you can list the proposed files for user selection. You could use ~/dustbin/$deletiondate if you wanted to make it possible to roll back to earlier versions.
You could do a cron job that would periodically remove old files and the directories (if empty).

Resources