9

Unix script to delete the file if it contains a single line

 3 years ago
source link: https://www.codesd.com/item/unix-script-to-delete-the-file-if-it-contains-a-single-line.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Unix script to delete the file if it contains a single line

advertisements

Consider I have a file abcde.txt which may contain one or more lines of text. I want a script that will DELETE the file if it contains single line.

Something like, if 'wc -l abscde.txt' = 1 then rm abscde.txt

My system : Solaris


Here's a simple bash script:

#!/bin/bash

LINECOUNT=`wc -l abscde.txt | cut -f1 -d' '`

if [[ $LINECOUNT == 1 ]]; then
   rm -f abscde.txt
fi

Tags unix

Related Articles

Unix script to delete the first line of a file on a Mac

On my Mac, when I try to run: sed -i 2d file.csv from the answer to Unix script to remove the first line of a CSV file, I get the following error: sed: 1: "file.csv": invalid command code f What can I do if I would like to delete the first two l

bat script to delete the file more than 7 days

I have a bat script in which gets all files in a folder and then converts this folder with its contents into a one RAR file. This script also adds the current date once it makes a copy and moves it this file into a backup folder. I'm planning to have

Apple Script to delete the file

I'm trying to write an applescript to delete my sleepimage file. The terminal command to do sudo rm /private/var/vm/sleepimage Clearly I cannot just type that into the applescript editor, so I've been using this do shell script rm / private / var / v

Linux script that will delete the files from the previous month

I am new in linux scripting and I have never created .sh file before. I need to create a script that will delete all the files which are from previous month. I have gone through the find /path/to/files* -mtime +5 -exec rm {} \; but find useless as da

bash shell script to delete the directory only if there are no files

Ok so I am writing a shell script to delete a directory but only if there are no files inside. what I want to do is have an if statement that will check if there are files in the directory and if there are files ask the user if they want to delete th

Unix command to delete all files in a directory but keep the directory

I am looking for a unix command to delete all files within a directory without deleting the directory itself. (note the directory does not contain subdirectories). rm -i <directory>/* this should do the trick EDIT: added -i just in case (safety firs

The os.rename python module deletes the files

I've wrote a little program to rename a big bunch of files in python #!/usr/local/bin/python3 import os import glob import sys script, arg1 = sys.argv f = open(arg1) # Create a filelist using glob filelist = glob.glob('*.avi') filelist.sort() for fil

Delete the file if it is not currently accessible

I am looking for a solution I need to delete log files, but there might be a possibility that they are being accessed at the moment the delete call is made. By being accessed, I mean a process is either reading or writing to the file. In such cases,

Run the download and delete the file when notifying inotifywait

I have a looping script that uploads a file to a server and upon completion returns a JSON object to the terminal on success with assorted information about the file/upload. Is there a way I can have a 2nd script running that watches for the confirma

Git: How to delete the file from the index without deleting files from a repository

When you use git rm --cached myfile it doesn't delete from the local filesystem, which is the goal. But if you've already versioned and committed the file, pushed it to a central repository, and pulled it into yet another repository before using the

Delete the file with the same name contained in different subfolders

For some reason, I have a copy of dismhost.exe in a lot of folders, inside temp folder; what I want to do is delete every instance of it, which are inside folders inside temp. So the structure is as follows: /temp /folder1 dismhost.exe /folder2 dismh

delete the file from the web server by downloading it

What's happen when FTP user delete some file at the same time some one downloading it? my server language is PHP. thanksOn a Windows server, you can't remove a file while it's in use by another process. On Unix systems, the file will be removed, but

Script to move the file from one network to another (UNC paths)

Could anyone please help in modifying my script to move the files from one network to another (using UNC paths)? I have tried like below (sample script), but its showing error like: "Syntax Incorrect, Path not found etc". Also please let me know

Script to delete specific files from all zip archives in a directory

I'd like to delete the file with a name, say file.xml, from all zip archives in a given directory. Any simple solution is welcome, whether it's a bash script, python code or whatever. Thanks!Something like this would do it recursively in a dir #!/bin

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK