BASH Copying files from Downloads

I have created a script that will check if the file is in Downloads folder. Then it will copy the file to the location needed and then rename this to something else.

I have done this by:

if [ -f /home/$username/Downloads/Framework\ Code\ Note\ $1.txt ]; then 
cp /home/$username/Downloads/Framework\ Code\ Note\ $1.txt /home/$username/Documents/TASKS/Framework/$3/$2/
mv Framework\ Code\ Note\ $1.odt Framework\ Code\ Note\ $2.odt
    echo "The file does not exist yet Download the latest notes from your email"

The problem is I usually have different versions of the same file name and need to download the latest file from the email. So the names become Framework Code Note 10.txt, Framework Code Note 10(1).txt Framework Code Note 10(2).txt etc...

Please can you help me to modify this or let me know how to modify the above code to only pull the latest Notes from my Downloads folder (if possible)?



To get the latest version of your file (i.e. Framework Code Note 10(X).txt, where X is the greatest), you can use the following :

IFS=$'\n' MYARRAY=($( printf "%s\n" "/path/to/downloads/Framework Code Note $1"*.txt | sort -nr -t '(' -k 2 ))
cp "${MYARRAY[0]}" /my/destination/folder

When you break it down :

  • printf "%s\n" "/path/to/downloads/Framework Code Note $1"*.txt finds all files in the downloads directory that start by "Framework Code Note Y", where Y is the value of $1. That way, Framework Code Note 10(X).txt and Framework Code Note 10.txt are matched.

  • sort -nr -t '(' -k 2 sorts the resulting files in numerical order (-n) considering the 2nd column (k 2). The separator for each column is '(' (-t '('), and the result is then reversed so that the first result has the highest number (-r)

  • The output of the command is stored in $MYARRAY. You then extract the highest version number when reading the 1st element of the array

  • IFS=$'\n': sets Internal Field Separator to \n, only for this line. This variable is used by bash to know how arguments are separated, its default value is \n\t.This means that when we allocate values to $MYARRAY, if a name contains an empty space, it would be treated as a separator (i.e, if you have only Framework Code Note 10.txt, Framework will be stored to MYARRAY[0], Code to MYARRAY[1], etc). As the attribution is done inline, $IFS is restored to its default value after execution

Need Your Help

Windows Phone Deserialize to List JSON .NET

c# .net json windows-phone-7 deserialization

I have problem deserialize string with JSON.Net on WP7. I have this JSON string:

Wordpress CSS doesn't work in Internet Explorer

css wordpress internet-explorer

Something seems wrong when i browse my site with IE 9-8-7. It is rendered perfectly with Chrome, Safari, Firefox and Opera. What can be the reason? Thank you.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.