tomato_soup
tomato_soup

Reputation: 354

Why the weird timestamp-like inclusion when piping ls to mkdir?

My computer is running Scientific Linux release 6.5 (Carbon). I want to create a new set of directories in

/newset/

with the same names as another set of existing subdirectories in

/oldset/

Here are the subdirectories in /oldset/

$ ls /oldset/
A/
B/
C/

I tried this

$ cd /newset/
$ ls /oldset/ | xargs mkdir

Which makes directories with these names

$ ls /newset/
?[0m?[38;5;27mA?[0m/
?[38;5;27mB?[0m/
?[38;5;27mC?[0m/
?[m/

Not what I'm expecting. I also tried doing a for loop through the ls output with mkdir and got the same result. Can someone explain why the weird result?

Upvotes: 0

Views: 67

Answers (2)

chepner
chepner

Reputation: 531808

Don't use ls in the first place; use an array:

names=( /oldset/* )
cd /newset
mkdir "${names[@]#/oldset/}"

or use a loop, although this requires multiple calls to mkdir instead of just one.

for d in /oldset/*/; do
    mkdir /newset/"${d#/oldset}"
done

Upvotes: 2

SaintHax
SaintHax

Reputation: 1943

To me it looks like you are running ls aliased to ls -F --color. I'm not at a box right now, but I think that's the right setting. I also don't know why you are using xargs? I'd use command substitution, but as Andrew pointed out, back-slash ls to turn off/ignore the alias.

 mkdir $( \ls /oldset)

Upvotes: 1

Related Questions