-2

Using GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)

I am a bash scripting novice, not sure where to begin except at shebang #!. The following command.

  • touch -a -m -t 201501010000.00 somefile.txt, will modify "somefile.txt" access time and modification time.
  • Is there a way to have a bash script?

    1. Operating within the directory "/mnt/harddrive/BASE/"

    2. Prompt user input for. "somefilename.txt", or "somedirectoryname".

    3. Prompt user input for. "datetime sequence". Instead of using the current time-stamp, explicitly specify the time/date using -t and -d options.
    4. Recursively change/modify. "atime", "mtime" on "sub-directory’s" within the BASE directory and "files" within that "sub-directory". AND
    5. Change/modify. "atime", "mtime" of "somefilename.txt" located in the "/mnt/hardrive/BASE/" directory.

    Optional 6. Append "mtime" to "somefilename" and "somedirectoryname" before the file extension. ie: "somefilename-01-01-2015.txt" or "somedirectoryname-01-01-2015". Prompt user: do you want to append "mtime" to "somefilename.txt" "YES/NO" if "YES/NO" continue.

    1. stat the directory and file output to the console or "/tmp" directory text file and display with cat then delete the "sometmpfile" rm -r.
  • Welcome to U & L SE. Try splitting every point of your question into a separate question and try asking it for better responses. Also, check the already existing questions and if you still feel if your question is not answered, post a new question by adding a link to the questions that you referred to. – Ramesh Jan 21 '15 at 18:00
  • 4. and 5. apply either to the directory or the file given in 2., so actually they are like 4a), 4b), correct? – nlu Jan 21 '15 at 20:29
  • "Is there a way to have a bash script?" - yes there is. You might want to hit up a bash scripting primer to get the basics under your belt. – datUser Jan 21 '15 at 20:48
  • @nlu yes, rootdirectory/file. directory/file, /sub-directory/file. Does that clarify anything? – Off Grid Jan 22 '15 at 14:31
  • @datUser, I knew this could be accomplished with bash, I have read the bash scripting primer, as i said I am a novice, thank you for the suggestion. – Off Grid Jan 22 '15 at 14:31
0

It could look like this:

#!/bin/bash

# 1. change directory
cd "/mnt/harddrive/BASE/" 

# 2. prompt for name of file or directory
echo -n "file or directory name: "
# ...  and read it
read HANDLE

# 2. b - check if it exists and is readable
if [ ! -r "$HANDLE" ] 
then
    echo "$HANDLE is not readable";
    # if not, exit with an exit code != 0
    exit 2;
fi

# 3. prompt for datetime
echo -n "datetime of file/directory: "
# ... and read it
read TIMESTAMP

# 4. set datetime for HANDLE (file or directory + files) 
find $HANDLE | xargs touch -a -m -t "$TIMESTAMP"

# 5. ask, if the name should be changed
echo -n "change name of file by appending mtime to the name (y/n)?: "
# ... and read it
read YES_NO
if [ "$YES_NO" == "y" ]
then
    # get yyyy-mm-dd of modification time 
    SUFFIX_TS=$(stat -c "%y" $HANDLE  | cut -f 1 -d" ")
    # rename, supposed, the suffix is always .txt
    mv $HANDLE $(basename $HANDLE txt)-$SUFFIX_TS.txt
    # let HANDLE hold the name for further processing
    HANDLE=$HANDLE-$TIMESTAMP.$SUFFIX
fi

# 7. stat to console
stat $HANDLE

This is just partly tested, but should be a start.

To get a an understanding of what is happening here, you should look up the following commands: echo, read, test, cut, touch, find, xargs

Besides you should understand several basic bash concepts, i.e. parameter substitution, command substitution and pipes.

  • Thank you, it's a great start, I will check your suggestions and begin with using your first example answer, I will reply with the outcome and further additions. Thank you again! – Off Grid Jan 22 '15 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.