1

After I ls and see the list, is there anyway to process a document without typing its name?

For example instead of typing rm damnSoLongFileName, can I type something like rm [1]?

I mean is there anyway to use ls like array?

4 Answers 4

3

Here is a one liner:

for something in $( ls );do echo $something;done

Or using select:

select item in $( ls );do echo $item;done

You could replace echo with rm and it would remove the item that you selected. You could then press ctrl+c to stop the loop.

Output:

bob@bob-p7-1298c:~$ select item in $( ls );do echo $item;done
1) adifferentlikethis    6) Documents       11) initramfs       16) MESVG20.xlsx    21) out.png     26) racket      31) t~          36) Untitled        41) VMs
2) bash_speakit      7) Downloads       12) irssi_log       17) MLGVG17.xlsx    22) output.mp3      27) run.py      32) Templates       37) Document
3) color_img.jpg     8) ec2         13) likethis        18) MLRVG17.xlsx    23) php5        28) run.sh      33) test        38) 1
4) DATES         9) examples.desktop    14) lpr         19) Music       24) Pictures        29) space.txt       34) t.sh        39) Videos
5) Desktop      10) grub.iso        15) MEEVG19.xlsx    20) nano.save       25) Public      30) t           35) Untitled-2.pd   40) VirtualBox
#? 

I then enter a number and it will echo it back to me.

#? 2
bash_speakit

Another Possibility:

thearray=( $(ls) )
echo "${thearray[2]}"

Output:

color_img.jpg
2

You could use tab-completion for long filenames. For your example type something like rm damn<TAB>.

1

From the "I mean is there anyway to use ls like array?" part I would assume that the question is about simply streaming the ls results into some other command thus I'm replying with a link to Xargs and a simple example usage of it:

find -name '*.txt' -print0 | xargs -0 rm

Check man find for more information about how to find, filter and stream the results using find command and man xargs for a full manual about xargs. The above example is a simple classic case of -print0 + -0 combination of arguments between the two using pipe.

0

No doubt unorthodox, but if you copy the script below into an empty file, save it as ls_2 in ~/bin, make it executable, and run it by the command (from anywhere):

ls_2

it will show a list like:

jacob@jacob-System-Product-Name:~/Bureaublad/all kinds of crap$ ls_2
1. nog te doen.odt
2. otto
3. GW_site_werkmap
4. sanel
5. blacklist.2
6. sanel_edited
7. sanel_gigue.mp3
8. Sprachmemo_011[1].m4a
9. PRG001
10. crap

please enter numbers to remove (separated by a comma): 1,3,5

Just enter the numbers and the items (both directories and files) will be removed.

Like the "real" ls command, it can be run from the current directory (just ls_2) or with a directory as argument (ls_2 /path/to/directory).

The script:

#!/usr/bin/env python3
import os
import shutil
import sys

try:
    currdir = sys.argv[1]
except IndexError:
    currdir = os.getcwd()

items = os.listdir(currdir)
for i in range(len(items)):
    print(str(i+1)+".", items[i])

remove = input("\nplease enter numbers to remove (separated by a comma): ")
indices = [int(it) for it in remove.split(",")]
for i in indices:
    file = currdir+"/"+items[i-1]
    try:
        os.remove(file)
    except IsADirectoryError:
        shutil.rmtree(file)
11
  • this requires non standard interpreter Dec 16, 2014 at 14:26
  • 1
    @МалъСкрылевъ There is no Ubuntu without python. Dec 16, 2014 at 14:27
  • that does not mean that user will use it as a shell Dec 16, 2014 at 14:27
  • @МалъСкрылевъ ??? python will be called anyway, since it is executable and in the shebang. the user will not be bothered to do anything. Dec 16, 2014 at 14:29
  • 1
    @МалъСкрылевъ Since this is AskUbuntu we may assume python is installed, else the system is broken. Dec 16, 2014 at 14:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .