Manipulation of file/folder with output of command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Manipulation of file/folder with output of command
# 1  
Old 07-05-2014
Manipulation of file/folder with output of command

I like organising some of my folders in the format of yyyymmdd to keep them in chronological order.
For example, today which is 5th July 2014, I made a folder named 20140705.
I want to manipulate these folders from a shell script, but I cannot find out how to do it without typing the literal name of the folder, such as the following command.

Code:
mv 20140705 sommelier

This is not what I want to do because when I make a new folder in the future, I can't use the same script without modifying it.
I know that the date command can create a formatted output of today's date,

Code:
date +"20%y%m%d"

which will essentially give me the name of my folder I created today and will also work the same way in the future.
I tried using the output of this command to move today's folder to somedir, for example by using a pipe "|", but it didn't work.
Is there a simple way how to do what I want to do, or am I doing something that cannot be done by shell script?

Last edited by Don Cragun; 07-05-2014 at 09:06 PM.. Reason: Add CODE tags.
# 2  
Old 07-05-2014
You didn't say what shell you're using. With any shell that conforms to the POSIX standards, try:
Code:
mv $(date +"%Y%m%d") somedir

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 07-05-2014
You should post the code you have tried so we can see what you are doing wrong.

This will do what you have described.

Code:
mv `date +"%Y%m%d"` sommelier


Last edited by mjf; 07-05-2014 at 09:13 PM.. Reason: Replaced hardcoded "20" with %Y
This User Gave Thanks to mjf For This Post:
# 4  
Old 07-06-2014
Dear Don, mjf

Both seems to work, thank you.
Somehow the missing "Code:" is also fixed without me doing anything, thank you for this as well.
Next time I will remember to mention that I am using bash when I ask questions.
# 5  
Old 07-06-2014
Quote:
Originally Posted by Gen12345
Both seems to work, thank you.
Both are working, but you should use Dons suggestion in favour of mjfs: there is a mechanism called "process substitution" in bourne-like shells (Bourne-Shell, bash and ksh, to name the most popular ones). It does the following: take a command, run it in a subshell, then use the output of this command in the current commandline:

Code:
mv $(date +'.....') somewhere

This first runs date +'.....', which will display a string, say "20140102". This string now replaces the the command it comes from on the command line, so effectively:

Code:
mv 20140102 somewhere

is executed. So far, this is equally the case for both constructs:

Code:
mv $(date +'.....') somewhere
mv `date +'.....'` somewhere

The difference is, that the latter is the historic form, which is only supported for backwards compatibility. The modern - and POSIX-standardised - form to write this is the first. The modern form also has some subtle advantages, like the ability to be nested:

Code:
mv $(command1 | command2 $(command3) ) somewhere

This could not be written using backticks at all.

I hope this helps.

bakunin

/PS:

Quote:
Somehow the missing "Code:" is also fixed without me doing anything, thank you for this as well.
This is because Don Cragun edited them into your post, as you can see at the bottom of your post if you scroll up.

bakunin
This User Gave Thanks to bakunin For This Post:
# 6  
Old 07-07-2014
Dear Bakunin,

This is also something useful to know.
Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert title as output of command to appended file if no output from command

I am using UNIX to create a script on our system. I have setup my commands to append their output to an outage file. However, some of the commands return no output and so I would like something to take their place. What I need The following command is placed at the prompt: TICLI... (4 Replies)
Discussion started by: jbrass
4 Replies

2. Red Hat

Command understanding the output file destination in case of standard output!!!!!

I ran the following command. cat abc.c > abc.c I got message the following message from command cat: cat: abc.c : input file is same as the output file How the command came to know of the destination file name as the command is sending output to standard file. (3 Replies)
Discussion started by: ravisingh
3 Replies

3. UNIX for Dummies Questions & Answers

Tar command to preserve the folder/file arrangement

Hi, I do have question for un tar a file. I have several 'tar'ed files. For example: SRS.tar.bz2. I was trying to untar them in a linux server using the command: tar xvjf SRS.tar.bz2 It worked perfectly. but when I open this file in my mac computer all the files are extracted into a... (7 Replies)
Discussion started by: Lucky Ali
7 Replies

4. UNIX for Dummies Questions & Answers

Using any file in folder as command input

Hi all, I have a command I need to run on a large number of folders. It needs only one input, namely one of the 150 files in the folder (doesn't matter which one): command filenameThe problem is that the names of the files are random, so I was trying to just get the first file by using list and... (5 Replies)
Discussion started by: Linnnnn
5 Replies

5. Shell Programming and Scripting

Command output string manipulation possible in one line?

This has been bothering me for 3 days. $> hostname cepsun64amd And I just want "cepsun", I would normally do h=`hostname`; ${h%%64*} But I am looking for a one-liner just for my own knowledge, because if there is a way to do this, I should know it by now. Anyway, so is this... (2 Replies)
Discussion started by: Ryan.
2 Replies

6. Shell Programming and Scripting

Bash folder manipulation - selecting/copying specified files

Bash/scripting newbie here - I feel this might be a trivial problem, but I'm not sure how to tackle it. I've got a folder of a year's worth of files, with some random number of files generated every day of the year (but at least one per day). I'm writing a script to automatically grab the file with... (6 Replies)
Discussion started by: WildGooseChased
6 Replies

7. UNIX for Advanced & Expert Users

Unix Command to rename a file in a zipped folder

Hi, I am into automation work. Is there any UNIX command to rename a file in a zipped folder, without unzipping it..??? Thanks in advance.. (1 Reply)
Discussion started by: Victoria.Sam
1 Replies

8. Shell Programming and Scripting

Moving 100K file to another folder using 1 command

Hi, I need to move 1000s of files from one folder to another. Actually there are 100K+ files. Source dir : source1 Target dir : target1 Now if try cp or mv commands I am getting an error message : Argument List too long. I tried to do it by the time the files are created in the source... (6 Replies)
Discussion started by: unx100
6 Replies

9. Shell Programming and Scripting

File Management: How do I move all JPGS in a folder structure to a single folder?

This is the file structure: DESKTOP/Root of Photo Folders/Folder1qweqwasdfsd/*jpg DESKTOP/Root of Photo Folders/Folder2asdasdasd/*jpg DESKTOP/Root of Photo Folders/Folder3asdadfhgasdf/*jpg DESKTOP/Root of Photo Folders/Folder4qwetwdfsdfg/*jpg DESKTOP/Root of Photo... (4 Replies)
Discussion started by: guptaxpn
4 Replies

10. UNIX for Dummies Questions & Answers

Disk Usage in GB and Unix command to find the biggest file/folder

Hi All, Please help me out 1) Command to find the disk usage in GB. I know that du -k will give in kilobites. 2) How to find the Biggest file/folder in a given set of files/folders. Thanks in advance Regards, Manas (8 Replies)
Discussion started by: manas6
8 Replies
Login or Register to Ask a Question