i want to copy the output of ls -l command into a file say "xyz". i know i have to take the output of ls -l and then send it to cp command using pipe ie output of ls-l will be used as an input for cp command
$ls-l
-rw-r--r-- 1 1030082 oinstall 167 Mar 27 10:36 \
-rw-r--r-- 1 1030082 oinstall 167 Mar 27 10:36 a1
-rw-r--r-- 1 1030082 oinstall 103 Mar 27 12:40 a2
-rw-r--r-- 1 1030082 oinstall 30 Mar 27 12:43 a3
-rw-r--r-- 1 1030082 oinstall 21 Mar 27 13:47 a4
drwxr-xr-x 3 1030082 oinstall 4096 Mar 27 16:54 dir1
-rw-r--r-- 1 1030082 oinstall 94 Mar 27 13:53 unix
-rw-r--r-- 1 1030082 oinstall 105 Mar 27 10:33 xyz
Output to file "test.txt":
ls -l > text.txt
Sumary of pipes:
> Save output to a file.
>> Append output to a file.
< Read input from a file.
2> Redirect error messages.
| Send the output from one program as input to another program.
If you are trying to copy the files that are obtained in directory listing into some other directory, then it will be as below
for i in $(ls -al | awk ' { print $9} ' ); do cp -rf $i $destination ; done;
If you are simple wanting to copy the output of ls -al into some file, then do
ls -al > file
If you're looking for something in file details (time, rights, etc...)
cp `ls -l | grep "xxx" | awk '{print $9}'` path_to_your_dest_directory
Just replace xxx with what you're looking for and your destination path.
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.