79785

How can i format the output of stat expression in Linux Gnome Terminal?

I am really newbie in Linux(Fedora-20) and I am trying to learn basics I have the following command

echo "`stat -c "The file "%n" was modified on ""%y" *Des*`"

This command returns me this output

The file Desktop was modified on 2014-11-01 18:23:29.410148517 +0000

I want to format it as this:

The file Desktop was modified on 2014-11-01 at 18:23

How can I do this?

Answer1:

You can't really do that with stat (unless you have a smart version of stat I'm not aware of).

With date

Very likely, your date is smart enough and handles the -r switch.

date -r Desktop +"The file Desktop was modified on %F at %R"

Because of your glob, you'll need a loop to handle all files that match *Des* (in Bash):

shopt -s nullglob for file in *Des*; do date -r "$file" +"The file ${file//%/%%} was modified on %F at %R" done

With find

Very likely your find has a rich -printf option:

find . -maxdepth 1 -name '*Des*' -printf 'The file %f was modified on %TY-%Tm-%Td at %TH:%TM\n'

I want to use stat

(because your date doesn't handle the -r switch, you don't want to use find or just because you like using as most tools as possible to impress your little sister). Well, in that case, the safest thing to do is:

date -d "@$(stat -c '%Y' Desktop)" +"The file Desktop was modified on %F at %R"

and with your glob requirement (in Bash):

shopt -s nullglob for file in *Des*; do date -d "@$(stat -c '%Y' -- "$file")" +"The file ${file//%/%%} was modified on %F at %R" done

Answer2:

stat -c "The file "%n" was modified on ""%y" *Des* | awk 'BEGIN{OFS=" "}{for(i=1;i<=7;++i)printf("%s ",$i)}{print "at " substr($8,0,6)}'

I have use here awk modify your code. what i have done in this code, from field 1,7 i printed it using for loop, i need to modify field 8, so i used substr to extract 1st 5 character.

Recommend

  • Is there a way to rename directories recursively without using sed or rename?
  • python - terminate child process when script invoked from bash
  • python import fails when called from PHP
  • Color syntax highlighting in Bash while using R
  • Use query params of parent route to refresh the model in subroute
  • Gem not installing package
  • reset jquery smartwizard
  • How to run bash commands like “npm install” on complie
  • Jquery resizable reposition handle after scroll
  • Is there a way to link a linux's thread TID and a pthread_t “thread ID”
  • 'doc_del_count' bigger than 'doc_count' on CouchDB
  • if some function is not optimized does it mean that all functions where it is declared are not optim
  • How can I get the full list of running processes on a Mac from a python app
  • OSX - always hide certain files
  • Hide HTML elements without javascript, only CSS
  • Get specific string
  • Copy to all folders batch file?
  • Tamper-proof configuration files in .NET?
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Moving mysql files across servers
  • Cancel a live stream “fast motion” catch-up in Flash
  • Extracting HTML between tags
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Illegal mix of collations for operation for date/time comparison
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • WinForms: two way TextBox problem
  • Deserializing XML into class C#
  • Release, debug version and Authorization Google?
  • Font Awesome Showing Box instead of Icons
  • How do you troubleshoot character encoding problems?
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • How to include full .NET prerequisite for Wix Burn installer
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • Is it possible to post an object from jquery to bottle.py?
  • costura.fody for a dll that references another dll
  • Python/Django TangoWithDjango Models and Databases
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize