Learn about Centmin Mod LEMP Stack today
Register Now

Sysadmin SSH - How to run a list of commands in a single go?

Discussion in 'System Administration' started by Jon Snow, May 17, 2023.

  1. Jon Snow

    Jon Snow Active Member

    727
    150
    43
    Jun 30, 2017
    Ratings:
    +215
    Local Time:
    12:08 AM
    Nginx 1.13.9
    MariaDB 10.1.31
    I have about 100 files I need to delete from an even larger folder. Finding each one individually will be a pain just to delete via FTP and I may have to do this often.

    I was wondering if there is a simple way to do it in a single command via command line if I have the list with each path and a delete command in a single line.

     
  2. Jon Snow

    Jon Snow Active Member

    727
    150
    43
    Jun 30, 2017
    Ratings:
    +215
    Local Time:
    12:08 AM
    Nginx 1.13.9
    MariaDB 10.1.31
    Maybe it's something that can be done with Xshell's Compose Pane?
     
  3. eva2000

    eva2000 Administrator Staff Member

    50,870
    11,786
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,240
    Local Time:
    2:08 PM
    Nginx 1.25.x
    MariaDB 10.x
    If you have list of all 100 full paths to the files you can just put into a shell script and prepend the linux rm -f command to each full path and run the script

    so rmfiles.sh shell script with contents of for all 100 files one on each line
    Code (Text):
    #!/bin/bash
    rm -f /full/path/to/file1.txt
    rm -f /full/path/to/file2.txt
    rm -f /full/path/to/file100.txt
    

    then give executable permissions and run the file
    Code (Text):
    chmod +x rmfiles.sh
    ./rmfiles.sh
    


    rm command help info
    Code (Text):
    rm --help
    Usage: rm [OPTION]... [FILE]...
    Remove (unlink) the FILE(s).
    
      -f, --force           ignore nonexistent files and arguments, never prompt
      -i                    prompt before every removal
      -I                    prompt once before removing more than three files, or
                              when removing recursively; less intrusive than -i,
                              while still giving protection against most mistakes
          --interactive[=WHEN]  prompt according to WHEN: never, once (-I), or
                              always (-i); without WHEN, prompt always
          --one-file-system  when removing a hierarchy recursively, skip any
                              directory that is on a file system different from
                              that of the corresponding command line argument
          --no-preserve-root  do not treat '/' specially
          --preserve-root[=all]  do not remove '/' (default);
                                  with 'all', reject any command line argument
                                  on a separate device from its parent
      -r, -R, --recursive   remove directories and their contents recursively
      -d, --dir             remove empty directories
      -v, --verbose         explain what is being done
          --help     display this help and exit
          --version  output version information and exit
    
    By default, rm does not remove directories.  Use the --recursive (-r or -R)
    option to remove each listed directory, too, along with all of its contents.
    
    To remove a file whose name starts with a '-', for example '-foo',
    use one of these commands:
      rm -- -foo
    
      rm ./-foo
    
    Note that if you use rm to remove a file, it might be possible to recover
    some of its contents, given sufficient expertise and/or time.  For greater
    assurance that the contents are truly unrecoverable, consider using shred.
    
    GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
    Full documentation at: <https://www.gnu.org/software/coreutils/rm>
    or available locally via: info '(coreutils) rm invocation'