Ok, so I have a few files in my directory which follows the following pattern:
TEST_20150130.txt
TEST_20150202.txt
TEST_20150203.txt
TEST_20150204.txt
TEST_RESULT_20150130.csv
TEST_RESULT_20150202.csv
TEST_RESULT_20150203.csv
TEST_RESULT_20150204.csv
Now I want to apply the command unix2dos, but only on TEST_20150204.txt and TEST_RESULT_20150204.csv
Would anyone be able to suggest what would be the easiest one liner script that could do this?
The easiest (but not necessarily the most robust):
unix2dos `\ls -rtd DIR_NAME/* | tail -2`
This is easy and will surely work on your example, but you should also be aware that in general you shouldn't parse the output of ls like that.
You can use below command too... Depends you only want to modify today's file or only last file ...
unix2dos `find -maxdepth 1 -type f -daystart -mtime -1`
Related
I need to delete all files with a pattern name: 2020*.js
Inside a specific directory: server/db/migrations/
And then show what it have been deleted: `| xargs``
I'm trying this:
find . -name 'server/db/migrations/2020*.js' #-delete | xargs
But nothing is deleted, and shows nothing.
What I'm doing wrong?
The immediate problem is that -name only looks at the last component of the file name (so 2020xxx.js) and cannot match anything with a slash in it. You can use the -path predicate but the correct solution is to simply delete these files directly:
rm -v server/db/migrations/2020*.js
The find command is useful when you need to traverse subdirectories.
Also, piping the output from find to xargs does not do anything useful; if find prints the names by itself, xargs does not add any value, and if it doesn't, well, xargs can't do anything with an empty input.
If indeed you want to traverse subdirectories, try
find server/db/migrations/ -type f -name '2020*.js' -print -delete
If your shell supports ** you could equally use
rm -v server/db/migrations/**/2020*.js
which however has a robustness problem if there can be very many matching files (you get "command line too long"). In that scenario, probably fall back to find after all.
You're looking for something like this:
find server/db/migrations -type f -name '2020*.js' -delete -print
You have try this:
find . -name 'server/db/migrations/2020*.js' | xargs rm
I have a directory with this structure:
root
|-dir1
| |-pred_20181231.csv
|
|-dir2
| |-pred_20181234.csv
...
|-dir84
|-pred_2018123256.csv
I want to run a command that will rename all the pred_XXX.csv files to pred.csv.
How can I easily achieve that?
I have looked into the rename facility but I do not understand the perl expression syntax.
EDIT: I tried with this code: rename -n 's/\training_*.csv$/\training_history.csv/' *.csv but it did not work
Try with this command:
find root -type f -name "*.csv" -exec perl-rename 's/_\d+(\.csv)/$1/g' '{}' \;
Options used:
-type f to specify file or directory.
-name "*.csv" to only match files with extension csv
-exec\-execdir to execute a command, in this case, perl-rename
's/_\d+(\.csv)/$1/g' search a string like _20181234.csv and replace it with .csv, $1 means first group found.
NOTE
Depending in your S.O. you could use just rename instead of perl-rename.
Use some shell looping:
for file in **/*.csv
do
echo mv "$(dirname "$file")/$(basename "$file")" "$(dirname "$file")/pred.csv"
done
On modern shells ** is a wildcard that matches multiple directories in a hierarchy, an alternative to find, which is a fine solution too. I'm not sure if this should instead be /**/*.csv or /root/**/*.csv based on tree you provided, so I've put echo before the 'mv' to see what it's about to do. After making sure this is going to do what you expect it to do, remove the echo.
I wish to create a program that zips whatever file is created in the directory the find parameters specify, and run it as a background process. I heavily comment it to give a better idea of what I'm trying to achieve. I'm running this from my MacBook Pro terminal, OS X version 10.9
#!/bin/sh
#find file in directory listed below
#type f to omit directories or special files
#mtime/ctime is modified/created -0 days or less
#name is with the name given in double quotes
#asterik meaning any file name with any file extension
#use xargs to convert find sequence to a command for the line after pipe
find /Users/name/thisdirectory type f -ctime -0 -name "'*'.'*'" | xargs zip -
Maybe you're looking for this:
find /path/to/dir -type f -ctime -0 -name "*.*" | zip -# file.zip
If you read zip -h, it explains that -# is to read the filenames from standard input.
You don't need xargs here, the function to work with a list of files received from standard input is built into zip itself, similar to most compression tools like tar.
Btw, I think you want to change -ctime -0, because I don't think it can match anything this way...
I have a folder /var/backup where a cronjob saves a backup of a database/filesystem. It contains a latest.gz.zip and lots of older dumps which are names timestamp.gz.zip.
The folder ist getting bigger and bigger and I would like to create a bash script that does the following:
Keep latest.gz.zip
Keep the youngest 10 files
Delete all other files
Unfortunately, I'm not a good bash scripter so I have no idea where to start. Thanks for your help.
In zsh you can do most of it with expansion flags:
files=(*(.Om))
rm $files[1,-9]
Be careful with this command, you can check what matches were made with:
print -rl -- $files[1,-9]
You should learn to use the find command, possibly with xargs, that is something similar to
find /var/backup -type f -name 'foo' -mtime -20 -delete
or if your find doesn't have -delete:
find /var/backup -type f -name 'foo' -mtime -20 -print0 | xargs -0 rm -f
Of course you'll need to improve a lot, this is just to give ideas.
First, any support and help on this is largely appreciated.
I'm trying to write a simple Bash script (completely new to this) to replace a file in a given directory.
Basically, I need to write a script to replace the safari preference file, here's what I have..and what's not working for that matter:
#!/bin/bash
find /Files/ -iname "com.apple.Safari.plist" - print0 | xargs -I{} -0 -1 cp file /Users/{}/Library/Preferences
It errors out with the following:
find: -: unknown option
xargs: illegal option -- 1
Any thoughts, ideas, are greatly appreciated.
Thanks,
I couldn't understand what exactly you want to accomplish with this. As I understand, you would have this "com.apple.Safari.plist" in /Files/, is that correct?
And then you want to get this file into some place that, I assume, overwrites Safari's current plist file. Assuming you take ghostdog74's correct advice and remove the space between - print0, thus turning it into -print0, and then remove the -1 from xargs, as it doesn't exist, this is what would happen:
find would find your file in /Files/, and xargs would run this:
cp file /Users/com.apple.Safari.plist/Library/Preferences; It would then die, since it would not find a file called "file" or a directory named "/Users/com.apple.Safari.plist/".
That's most likely not what you want. :)
If you just want to copy the file to somewhere, why don't you just do cp /Files/com.apple.Safari.plist ~/Library/Preferences/ ?
Do you really need find and xargs in this case? Could you clarify?
No space between - print0. and since -1 is not an option, remove it and see.
find /Files/ -iname "com.apple.Safari.plist" -print0 | xargs -I{} -0 cp file /Users/{}/Library/Preferences