简体   繁体   中英

Linux CP using AWK output

I have been trying to learn more about Linux and have spent this morning focusing on the awk command. the command I have been trying to get to work is below.

ls -lRt lpftp.* | awk '{print $7, $9}' | mkdir -p $(awk '{print $1}') | ls -lRt lpftp.* | cp $(awk '{print $9, $7}')

Essentially I am trying to move each file in a directory into a sub directory based on that files last modified day. The command first prints only the files I want, then uses mkdir to create a folder based on the day of the month it was last modified. What I want to do after that is move each file into its associated directory, however as the command is now it moves every file into the 01 folder and prints out the following text

cp: 0653-436  12 is a directory.
    Specify -r or -R to copy.

once for every directory.

does anyone know how I can fix this issue? or if there is a better way to go about it?

ls -lRt lpftp.* | awk '{print $7, $9}' | while read day file ; do mkdir -p "$day"; cp "$file" "$day"; done

The commands between do and done will be executed for each line of output, with the first thing awk prints in the day variable and the second in file (per line). I used quotes here somewhat unnecessarily, as there will not be spaces in the variables given the method by which they are set.

The safest way to do something like this -- and the fastest to execute -- is to use awk on the data to output a shell script. In awk, print the mkdir and cp commands you expect to execute. Pipe the results into head (1) until you're satisfied. Maybe look at the whole thing in less (1). Then execute as follows:

ls -lRg lpfpt.* | awk script.awk | sh -ex

That will echo the commands to standard error, and stop on the first error. If you're super absolutely sure it's right, drop the x option.

The advantage of this approach over a loop or a bunch of subprocesses in awk (with the system function) is:

  • you can see what's going to happen, and what's happening
  • speed of execution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM