Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list files in directory using bash? [closed]

Tags:

bash

How to copy only the regular files in a directory (ignoring sub-directories and links) to the same destination? (bash on Linux) A very large number of files

like image 753
Arthur Avatar asked Sep 01 '11 01:09

Arthur


People also ask

How do I get a list of files in a directory in Bash?

Use the ls Command to List Directories in Bash. We use the ls command to list items in the current directory in Bash. However, we can use */ to print directories only since all directories finish in a / with the -d option to assure that only the directories' names are displayed rather than their contents.

How do you list only files in a directory in Linux?

Open the command-line shell and write the 'ls” command to list only directories. The output will show only the directories but not the files. To show the list of all files and folders in a Linux system, try the “ls” command along with the flag '-a” as shown below.


2 Answers

for file in /source/directory/* do     if [[ -f $file ]]; then         #copy stuff ....     fi done 
like image 107
Mu Qiao Avatar answered Oct 03 '22 17:10

Mu Qiao


To list regular files in /my/sourcedir/, not looking recursively in subdirs:

find /my/sourcedir/ -type f -maxdepth 1 

To copy these files to /my/destination/:

find /my/sourcedir/ -type f -maxdepth 1 -exec cp {} /my/destination/ \; 
like image 38
poplitea Avatar answered Oct 03 '22 18:10

poplitea